httpsrv - Simple Local HTTP Server
This article is automatically translated by LLM, so the translation may be inaccurate or incomplete. If you find any mistake, please let me know.
You can find the original article here .
httpsrv is a very simple small server, similar to python2
's SimpleHTTPServer
. You can use a simple command to temporarily turn a folder into an HTTP server, and then browse it in your browser.
Installation
npm i -g httpsrv
#或 yarn global add httpsrv
Usage Guide
Basic
First, use cd
to navigate to the folder you want, then enter
httpsrv . # . 代表當前資料夾
Then open your browser to localhost:3333 and you should see the file list in the current directory. This image is the hexo folder of this blog.
port&log
For example, this can open a server on localhost:8888 and log all requests in the console.
httpsrv . -p 8888 -l
Other Options
It is recommended to go directly to maple3142/httpsrv for more complete information.
Usage: httpsrv <basedir>
Options:
--help Show help [boolean]
--version Show version number [boolean]
--port, -p Port to listen [number] [default: 3333]
--log, -l Enable logger [boolean]
--cors, -c Access-Control-Allow-Origin header [string]
--fallback, -f A file will be send when 404, useful in SPA [string]
--indexhtml, -i Try to show index.html if exists [boolean]
Examples:
httpsrv . -p 8888 Start server on port 8888
Advanced Usage
Blog
This can also be used to test the website created by hexo. Let's use this blog as an example GitHub repo.
git clone https://github.com/maple3142/Blog.git
cd Blog
npm install
npm run build #一般來說是hexo generate, 不過我有把build增加一些東西(例如minify)
httpsrv public -i -f 404.html
The last line -i
automatically detects if there is an index.html
in the directory and serves it if found. -f 404.html
is the file that will be served when a file is not found (404). Note that 404.html
should not be written as public/404.html
.
WynnStats
WynnStats is a SPA practice I did before, and I plan to use it as an example.
git clone https://github.com/maple3142/WynnStats.git
cd WynnStats
npm install
npm run build
httpsrv dist -i -f index.html
Next, open localhost:3333, click any link, and press F5. You will find that everything works fine. The last line looks almost the same as above, mainly because of the -f index.html
function. Since SPA websites only have one index.html
, it will serve index.html
on 404. The routing logic is handled inside with JavaScript.