lwsjs / local-web-server

A lean, modular web server for rapid full-stack development.
MIT License
1.2k stars 85 forks source link

rewrite #80

Closed kfdorman closed 6 years ago

kfdorman commented 6 years ago

I am trying to use --rewrite to redirect traffic on a server. My intention is to serve the main site using local-web-server on port 8000, but to pass some urls to port 8080 on the same server where I have another node.js server listening for that traffic.

I issued the command:

ws --rewrite '/command/* -> http://192.168.2.6:8080/$1'

which prompted the message:

serving at http://192.168.2.6:8000

however when I visit the url:

http://192.168.2.6:8000/command/boost

I expected the url to be redirected to:

http://192.168.2.6:8080/boost

however I do not record the url being visited.

If I simply browse to http://192.168.2.1:8080/boost

the application listening correctly records the url being visited

I am quite new to this, but cannot work out what I am doing wrong. Are you able to advise?

Thanks

Karl

75lb commented 6 years ago

trying running ws with the --verbose flag? Does the output give any clues? In the verbose output, you should see proxy requests being made to http://192.168.2.6:8080..

75lb commented 6 years ago

I expected the url to be redirected to:

the request will not be redirected, if that's what you expected.. the request to http://192.168.2.6:8000 should be proxied (internally by ws) to http://192.168.2.6:8080..

kfdorman commented 6 years ago

Thanks for your help. The --verbose flag helped and I now have it working. However, I'm keen to run it from within a javascript script and I'm struggling to get that working.

To recap - running from the command line, I can issue the following command which works for me:

ws --rewrite '/command/* -> http://192.168.2.6:8080/$1'

I have created the lws.config.js files as follows:

module.exports = {
  rewrite: [
    {
      from: '/command/*',
      to: 'http://192.168.2.6:8080/$1'
    }
  ],
}

and have the following code in my working.js file which I run by issuing the command: node working

const LocalWebServer = require('local-web-server')
const localWebServer = new LocalWebServer()
const server = localWebServer.listen()
console.log("Local Web Server listening on http://192.168.2.6:8000")

When I do it this way, it still serves the website as expected, but proxy requests to the server listening on port 8080 don't arrive as expected.

I have tried to get verbose logging working in this method but adding the verbose flag just results in node.js errors.

Please let me know if you can see where I am going wrong

Karl

75lb commented 6 years ago

what node errors are you getting?

To see the verbose output, handle the verbose event.

server.on('verbose', (key, value) => {
    console.log(key, value)
})