How to create instant web server via command line?
|Some days back, i was working on a front end work with html, css and JavaScript but there was no backend. I was feeling very odd while seeing the html page url like file:///D:/…, then sudden one of my friend ask me the code on his ip. He want to see the webpages on his machine on local sever. So, i thought why don’t put this code on web server but i don’t want to use the Apache, tomcat server for this front end code only. So, i started searching some other way to create http server and i was very happy to got the solution.
There are 2 ways to create instant web server by command line –
1 – http-server – Its installation is done by the npm. You need to install node on your system.
Run the following command, once you have npm. -g make http-server available globally:
npm install http-server -g
Go to static folder where you wish to create local server and run command line with the following command –
http-server
It will show the message –
Starting up http-server, serving ./ on port:8080
Hit Ctrl-C to stop the server.
The default port used by http-server is 8080, but you can change this, and a few other options, via command line switches:
- -p to specify which port to listen on
- -a to tell it which IP address to bind to
- -i to configure whether to display autoIndex (defaults to true)
- -s or –silent to turn off console logging
- -h or –help to display this list of available commands
2- Python SimpleHTTPServer module – To start an instant web serve in the directory by command line use the follwing command –
python -m SimpleHTTPServer
This will publish the current directory on server, we can access it on port 8000, it is default port for python server. We can access it by our ip x.x.x.x.:8000 or 127.0.0.1:8000 etc.
Note- Python should be installed on system to use this feature.