lws-rewrite

raw JSON →
4.0.0 verified Sat Apr 25 auth: no javascript

lws-rewrite is a middleware plugin for lws (Local Web Server) that adds URL rewriting support. Current stable version is 4.0.0, released under the ISC license. It allows defining rewrite rules via command-line options to map source URLs to local or remote destinations, supporting pattern-based replacements. Key differentiator: integrates seamlessly with lws's plugin architecture and its wiki provides detailed usage examples. The project is maintained with regular updates, compatible with Node.js >=12.17.

error Error: Cannot find module 'lws-rewrite'
cause Package not installed or not in node_modules.
fix
Run 'npm install lws-rewrite' in your project.
error TypeError: lws_rewrite_1.default is not a constructor
cause Using CommonJS require() with an ESM-only package.
fix
Use ESM import: import Rewrite from 'lws-rewrite'
error lws: rewrite rule '/old-> /new' is invalid. Must contain ' -> ' (space-dash-greaterthan-space)
cause Rule syntax missing required spaces around '->'.
fix
Use correct format: '/old -> /new'
gotcha Rewrite rules must use '->' with spaces; missing spaces will cause the rule to be ignored.
fix Use format '/from -> /to' with spaces around '->'.
breaking Version 4.0.0 dropped support for Node.js <12.17 and switched to ESM-only.
fix Upgrade to Node.js >=12.17 and use ESM imports.
deprecated The '--rewrite' option replaces older '--map' or custom middleware; use '--rewrite' for current API.
fix Use '--rewrite' instead of deprecated options.
gotcha Rules are applied in order; first matching rule wins, which may lead to unexpected behavior if overlapping patterns.
fix Order rules from most specific to least specific.
gotcha Rewriting to remote destinations does not alter the Host header; remote server may reject requests.
fix Use lws proxy middleware for full proxy support.
npm install lws-rewrite
yarn add lws-rewrite
pnpm add lws-rewrite

Demonstrates programmatic usage of lws-rewrite plugin with lws, setting up rewrite rules to proxy API requests and redirect a path.

import lws from 'lws'
import Rewrite from 'lws-rewrite'

const server = lws.create({
  port: 8080,
  stack: [new Rewrite()],
  rewrite: [
    '/api/* -> http://localhost:3000/api/$1',
    '/old-path -> /new-path'
  ]
})

server.start().then(() => {
  console.log('Server started on http://localhost:8080')
})