I am looking for a way to run a local http-server with npm during development. I have seen there is http-server
but I have one additional requirement. I need to rewrite the url to my backend application with a proxy. It seems the standard http-server
that can be installed with npm is not able to do this. Is there any other plug-in that can do this?
Essentially what I need to something like this:
# serve files in local folder, but rewrite all request
# to /api to localhost:8080/
npm run some-http-server --rewrite "/api/(.*) localhost:8080/$1" --path .
So when I access localhost/index.html
is servers the local index.html file, but when I access localhost/api/foo
it rewrites that as localhost:8080/foo
and proxies it to my backend application running on port 8080
.
Does a solution like this already exist?
Installing HTTP server using NPM Run the command line/terminal on your system (it doesn't matter which directory you are currently in). Execute npm install -g http-server . Once NPM finishes, you have the tiny HTTP-server installed. That's it.
http-server is a simple, zero-configuration command-line static HTTP server. It is powerful enough for production usage, but it's simple and hackable enough to be used for testing, local development and learning.
Type npm init into your command prompt. This initializes, or sets up, your folder to run a node server.
npm run serve basically is just saying "npm please run the command I defined under the name serve in package. json" the same happens with npm run dev . Given this the commands can do the exact same thing, similar things, or very different things.
It looks like the local-web-server npm package is exactly what I was looking for.
Install it with:
sudo npm install -g local-web-server
Theb start it with:
ws -p 63342 -r '/api/*->http://localhost:8080/$1' -d myapp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With