Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm http server during development and rewrite url

Tags:

npm

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?

like image 691
lanoxx Avatar asked Apr 22 '16 11:04

lanoxx


People also ask

How do I start an HTTP server?

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.

What is HTTP server command?

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.

Which of the npm commands spin up the local server?

Type npm init into your command prompt. This initializes, or sets up, your folder to run a node server.

What is serve in npm?

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.


1 Answers

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
like image 154
lanoxx Avatar answered Sep 17 '22 18:09

lanoxx