I’ve searched for this three times this week, so I figured I’d better make sure I have a copy of it.
Python 2:
python -m SimpleHTTPServer 8000
Navigate to the project directory in the terminal and do that command. Then http://localhost:8000 will server up that directory (as in, it’s `index.html` file).
Python 3:
python3 -m http.server --cgi 8080
PHP:
php -S localhost:2222
npm:
npm i -g serve
serve
It can be very useful to spin up a quick server.
php -S localhost:8000
also works in recent versions.This command requires PHP 5.4.0 and above
Requires installation, but with Node:
Once installed, you can run from any directory:
I use “http-server” regularly. It is simpler and easy to remember.
okay i did as you commented in the terminal of my linux OS –
$ http-server
but i am getting error as-
“http-server: command not found”
Oh man. Where has this been all my life. This has been an immense time-saver for me, being able to spin up a standalone server for any given directory, pass some parameters — boom, local server to test on. Thanks for posting this!
@samar you need to install http-server first:
npm i -g http-server
You need npm to install http-server, and to run that npm command you need npm, and to have npm installed you need Node :)
Install Node
Install npm
install http-server
All 3 of them can be installed with only few lines…
So google them all.
npx serve
andnpx http-server
– both work if you have node installed with [email protected] or greater.npx
has been around since roughly 2017, and I believe [email protected] was the first version it could be used in (because that is the first version of node using [email protected] or greater)I always use live-server – Like http-server, but includes watching for changes
The node.js / NPM commands above can be achieved in one line as per title of article.
npx serve
Nice one, thanks for the wonderful work sir.
Running this
python3 -m http.server --cgi 8080
from any preferred file location works awseome,,,actually doing it via termux