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):
-pPort to use (defaults to 8080)
-aAddress to use (defaults to 0.0.0.0)
-dShow Directory listings (defaults to ' True ')
-iDisplay AutoIndex (defaults to ' True ')
-gOr 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.
-eor --ext Default file extension if none supplied (defaults to ' HTML ')
-sor --silent suppress log messages from output
--corsEnable CORS via the Access-Control-Allow-Origin header
-oOpen browser window after starting the server
-cSet 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.
-UOr use UTC time, --utc format in log messages.
-Por --proxy Proxies all requests which can ' t is resolved locally to the given URL. e.g.:-P http://someurl.com
-Sor --ssl Enable HTTPS
-Cor --cert Path to SSL cert file (DEFAULT:CERT.PEM).
-Kor --key Path to SSL key file (DEFAULT:KEY.PEM).
-ror --robots provide a/robots.txt (whose content defaults to ' user-agent: *\ndisallow:/')
-hor --help Print this list and exit.
Optional configuration:
-pThe port to use (default is 8080)
-aThe address to use (default is 0.0.0.0)
-dShow directory list (default = "True")
-iShow AutoIndex (default = "True")
-gor --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.
-eor --ext Default file name extension (if not provided) (default is ' HTML ')
-sor --silent suppress log messages from the output
--corsAccess-Control-Allow-OriginEnable Cors by title
-oOpen the browser window after starting the server
-cSets 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.
-Uor --utc use the UTC time format in the log message.
-POr the --proxy agent cannot resolve all requests locally for a given URL. For example:-P http://someurl.com
-Sor --ssl enable HTTPS.
-Cor --cert the path to the SSL certificate file (default: CERT.PEM).
-Kor --key the path to the SSL key file (default: KEY.PEM).
-ror --robots provide a/robots.txt (whose content defaults to ' user-agent:* \ ndisallow:/')
-hor --help print this list and exit.
Http-server Nodejs-based HTTP server