The HTTP server profile

  • Http-server is a simple command line with zero configuration
  • HTTP server, which is powerful enough for production and use, for local testing and development.

Sometimes we open a document using the file open the agreement, cannot send ajax requests, only request resources using the HTTP protocol, specific you can go to the difference between a reference (this document) (www.cnblogs.com/lishanlei/p)… , so we need to set up a local HTTP service, through IP and port number, to access resources.

I usually open it via vscode plugin or webstorm. Now it’s easy to create a local service using http-server.


1. Installation node. Js

Download the recommended version from the official website and install it. Run the node -v and NPM -v commands on the CLI to check whether the installation is successful.

2. Install the HTTP server

HTTP server – in detail, including parameters, such as: www.npmjs.com/package/htt… ; -g indicates a global installation. After that, HTTP -server can be used in any folder path.

npm install http-server -g

3. Enable the service

On the CLI, enter the target folder (that is, the folder where the service is to be started) and run the following command:

http-server

  • The local server is started with a default port of8080.

4. Access from each end

After the local server is started, the command line window displays the following address:

For native access, both addresses are acceptable; If the mobile phone or other device is connected, please use the IP address (http://10.134.253.8:8080), and the mobile phone or other device must be on the same LOCAL area network as the local computer.

shorthand

hs

Modify the port

Http-server -a 127.0.0.1 -p 8090


http-server [path] [options]

  • **** is the directory address, which can be omitted. By default, it is the path where CMD is opened

  • The -a parameter is the listening address

  • The -p argument is the listening port

The other parameters

parameter role
P or — port Port Settings, default is8080
-a The default listening address setting is0.0.0.0
-d Whether to display file list Defaulttrue
-i Display auto index defaulttrue
– g or — gzip The defaultfalse, when the filegzipVersion exists and request acceptedgzipWhen coded, it will serve./public/some-file.js.gzRather than./public/some-file.js
– or – e ext If the default file extension is not provided (the default ishtml)
-s 或者 –silent The output of console logs is disabled
– cors Allows cross-domain resource sharing
-o Open the default browser after starting the service
-c Set the cachecache-control max-age headeDuration (in seconds), for example:-c10is10Seconds, the default is3600Seconds, if you want to disable caching-c-1
-U 或者 –utc useUTCFormat, output time information on the console
P or — proxy Through aurlThe address of a resource that the proxy cannot resolve locally
-s or – SSL usehttpsagreement
– or – C cert sslPath to the certificate file. The default path iscert.pem
– or – key K sslKey file path
-h or — help Display help