Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ Options:
-d, --delay <livereolad delay> delay in ms before triggering live reload, default 0
-x, --proxy <upstreamurl> when file not found, proxy the request to another server
--proxypath <proxypath> only send to proxy when path starts with this pattern, default is "/", repeatable
--pathRewriteRule <regexp> when proxying, rewrite request path based on --pathRewriteRule and --pathRewriteTo, note that you must exact match the number with --pathRewriteTo
--pathRewriteTo <path> when proxying, rewrite request path based on --pathRewriteRule and --pathRewriteTo, note that you must exact match the number with --pathRewriteRule, repeatable


--no-reload disable live-reloading
-q, --quiet quiet mode with minimum log message
-o, --open [path] open browser automatically, optional path
Expand All @@ -74,6 +78,7 @@ Examples:
$ light-server -s . -x http://localhost:8000 --servePrefix /assets
$ light-server -s . -b 10.0.0.1
$ light-server -x http://localhost:9999 --proxypath "/api" -w "public/**"
$ light-server -x http://localhost:9999 --proxypath "/api" --pathRewriteRule "^/api" --pathRewriteTo "\"\""'
$ light-server -s static -w "**/*.css # # reloadcss"
$ light-server -c .lightserverrc
& light-server -s . -p 8000 -w "src/**/*.js # npm run js # no-reload"
Expand Down Expand Up @@ -179,6 +184,8 @@ To use a config file, create a json file and use `-c/--config`. The config templ
"delay": 0,
"proxy": "http://localhost:9999",
"proxypaths": [ "/api" ],
"pathRewriteRule": [ "^/api" ],
"pathRewriteTo": [ "" ],
"noReload": false,
"quiet": false,
"open": true,
Expand Down
14 changes: 14 additions & 0 deletions bin/light-server
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ function main (argv) {
.option('-d, --delay <livereolad delay>', 'delay in ms before triggering live reload, default 0', parseInt)
.option('-x, --proxy <upstreamurl>', 'when file not found, proxy the request to another server')
.option('--proxypath <proxypath>', 'only send to proxy when path starts with this pattern, default is "/", repeatable', collect, [])
.option('--pathRewriteRule <regexp>', 'when proxying, rewrite request path based on --pathRewriteRule and --pathRewriteTo, note that you must exact match the number with --pathRewriteTo, repeatable', collect, [])
.option('--pathRewriteTo <path>', 'when proxying, rewrite request path based on --pathRewriteRule and --pathRewriteTo, note that you must exact match the number with --pathRewriteRule, repeatable', collect, [])
.option('--no-reload', 'disable live-reloading')
.option('-q, --quiet', 'quiet mode with minimum log message')
.option('-o, --open [path]', 'open browser automatically, optional path')
Expand All @@ -46,6 +48,7 @@ function main (argv) {
console.log(' $ light-server -s . -x http://localhost:8000 --servePrefix /assets')
console.log(' $ light-server -s . -b 10.0.0.1')
console.log(' $ light-server -x http://localhost:9999 --proxypath "/api" -w "public/**"')
console.log(' $ light-server -x http://localhost:9999 --proxypath "/api" --pathRewriteRule "^/api" --pathRewriteTo "\"\""')
console.log(' $ light-server -s static -w "**/*.css # # reloadcss"')
console.log(' $ light-server -c .lightserverrc')
console.log('')
Expand Down Expand Up @@ -74,6 +77,8 @@ function main (argv) {
delay: 0,
bind: undefined,
proxypaths: ['/'],
pathRewriteRule: [],
pathRewriteTo: [],
watchexps: []
}
if (program.config) {
Expand All @@ -91,6 +96,8 @@ function main (argv) {
watchexps: program.watchexp.length ? program.watchexp : undefined,
proxy: program.proxy,
proxypaths: program.proxypath.length ? program.proxypath: undefined,
pathRewriteRule: program.pathRewriteRule.length ? program.pathRewriteRule: undefined,
pathRewriteTo: program.pathRewriteTo.length ? program.pathRewriteTo: undefined,
noReload: program.reload ? undefined : true,
quiet: program.quiet,
open: program.open,
Expand All @@ -105,6 +112,13 @@ function main (argv) {
process.exit(1)
}

if (options.pathRewriteRule.length > 1) {
if (options.pathRewriteRule.length !== options.pathRewriteTo.length) {
console.error('Please exact match the number of options of --pathRewriteRule and those of --pathRewriteTo.')
process.exit(1)
}
}

LightServer(options).start()
}

Expand Down
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,16 @@ LightServer.prototype.start = function () {

if (_this.options.serve) {
if (_this.options.proxy) {
var proxy = require('./proxy')
app.use(proxy(_this.options.proxy, _this.options.proxypaths).middleFunc)
var { createProxyMiddleware } = require('http-proxy-middleware')
var proxyOptions = {
target: _this.options.proxy,
changeOrigin: true,
pathRewrite: {},
}
_this.options.pathRewriteRule.forEach(function(rule, index) {
proxyOptions.pathRewrite[rule] = _this.options.pathRewriteTo[index]
})
app.use(createProxyMiddleware(_this.options.proxypaths, proxyOptions))
}

if (_this.options.historyindex) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"connect-injector": "^0.4.4",
"gaze": "^1.1.3",
"http-proxy": "^1.18.1",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could I remove this?

"http-proxy-middleware": "^1.0.6",
"morgan": "~1.10.0",
"opener": "^1.5.1",
"parseurl": "^1.3.3",
Expand Down
33 changes: 0 additions & 33 deletions proxy.js

This file was deleted.

Loading