Http-server the scene used:
- As a front-end classmate, want to run a piece of code, but there is no need to use Tomcat or Apache HTTP server, this time, a simple light-weight http-server can be done.
- After the development of the current end, we need to package the deployment, the general will generate a Dist folder, which contains some static files, when we run these static files directly in the editor, it is likely to appear "because its MIME type (' text/html ') is Not a supported stylesheet MIME-type, and strict MIME checking is enabled. " This type of error about MIME type, which is caused by the static file access restrictions, can be done with http-server at this point.
- In the normal development process, often encountered no use of any framework and tools, is to run a simple HTML page, you need to start a local server.
Sometimes the front end, want to run some code, but there is no need to use Tomcat or Apache HTTP server, this time a lightweight simple HTTP server can be done.
Http-server is a Nodejs-based HTTP server, its biggest advantage is: can make any directory into a server directory, completely aside from the background of heavy engineering, directly run the desired JS code.
Http-server Benefits:
- You can make any directory of the static resource file a directory of the server, completely throwing away the heavy work in the background and running the code you want directly.
Http-server Installation and Operation:
- Install node. js
- Install http-server using NPM/CNPM; run NPM install http-server-g
- Get started: Use the CD to jump to the folder you want to view, example: CD dist, and then place the folder on the local server. Start with Http-server, and the following code appears:
- Starting up Http-server, serving.
- Available on:
- http://127.0.0.1:8081
- http://10.1.1.232:8081
- Hit Ctrl-c to stop the server
The browser launches with http://127.0.0.1:8081 or http://10.1.1.232:8081 to see the code you want to run;
- When not used, the direct use of CTRL + C end can be;
Http-server Available parameters (Available Options):
-p
Port to use (defaults to 8080)
-a
Address to use (defaults to 0.0.0.0)
-d
Show Directory listings (defaults to ' True ')
-i
Display AutoIndex (defaults to ' True ')
-g
Or when --gzip
enabled (defaults to ' False ') it would serve in place of when ./public/some-file.js.gz
./public/some-file.js
a gzipped version of the file exist s and the request accepts gzip encoding.
-e
or --ext
Default file extension if none supplied (defaults to ' HTML ')
-s
or --silent
suppress log messages from output
--cors
Enable CORS via the Access-Control-Allow-Origin
header
-o
Open browser window after starting the server
-c
Set cache time (in seconds) for Cache-control max-age headers, e.g.-C10 for the seconds (defaults to ' 3600 '). To disable caching, use-c-1.
-U
Or use UTC time, --utc
format in log messages.
-P
or --proxy
Proxies all requests which can ' t is resolved locally to the given URL. e.g.:-P http://someurl.com
-S
or --ssl
Enable HTTPS
-C
or --cert
Path to SSL cert file (DEFAULT:CERT.PEM).
-K
or --key
Path to SSL key file (DEFAULT:KEY.PEM).
-r
or --robots
provide a/robots.txt (whose content defaults to ' user-agent: *\ndisallow:/')
-h
or --help
Print this list and exit.
Optional configuration:
-p
The port to use (default is 8080)
-a
The address to use (default is 0.0.0.0)
-d
Show directory list (default = "True")
-i
Show AutoIndex (default = "True")
-g
or --gzip
enabled (the default is "False"), it will be used in ./public/some-file.js.gz
place ./public/some-file.js
of the gzip compressed version of the file, and the request accepts gzip encoding.
-e
or --ext
Default file name extension (if not provided) (default is ' HTML ')
-s
or --silent
suppress log messages from the output
--cors
Access-Control-Allow-Origin
Enable Cors by title
-o
Open the browser window after starting the server
-c
Sets the cache control Max-age header's cache time (in seconds), for example-C10 10 seconds (default is ' 3600 '). To disable caching, use-c-1.
-U
or --utc
use the UTC time format in the log message.
-P
Or the --proxy
agent cannot resolve all requests locally for a given URL. For example:-P http://someurl.com
-S
or --ssl
enable HTTPS.
-C
or --cert
the path to the SSL certificate file (default: CERT.PEM).
-K
or --key
the path to the SSL key file (default: KEY.PEM).
-r
or --robots
provide a/robots.txt (whose content defaults to ' user-agent:* \ ndisallow:/')
-h
or --help
print this list and exit.
Http-server Nodejs-based HTTP server