Build the simplest and lightweight http server with nodejs
1. Introduction
Front-end programmers mainly focus on pages. You may not be able to use background languages such as. net, java, and php.
But do you always need to run and test the web pages you make? -- It is inevitable that http server is used. I used visual studio in the past. Although it is very useful and powerful, I developed an html, javascript, and css product. Why do I use this silly and rough stuff. Opening a disk is very slow and memory usage is extremely high. The C disk occupies several GB of space during installation.
Later, I changed to nodejs when I had nothing to do. You do not need to install any plug-ins. You only need to manually create three small files (only 2 kb in total) and run them quickly. It is very simple and lightweight. I would like to share it with you. Please skip this step!
2. Build in four steps
Step 1: Install nodejs:
Don't talk nonsense, go to www.nodejs.org to download and install.
Step 2: create two js files
Create two js files and save them as server. js and mine. js. The Code is as follows:
Server. js mine. js
I created a new "demo1" folder on the desktop and copied the two created js files.
Step 3: Create a. bat command file
Or the "demo1" folder, and then create a "startServer. bat" file in this folder. The file content is actually one sentence:
StartServer. bat
In this case, the demo1 folder contains three files:
Step 4: add your html file
In addition, the demo1folder needs to create a demotest1folder in this file, and then we add index.html for testing later.
Note: why is there a "test1" folder? Because the directory configured in "server. js" is "test1 ". If you are not pleasing to the eye, you can get rid of it, but make sure that the two names are the same.
If we have several test projects in demo1 and need to run them, you can enable "server. js" to modify the configuration, which is very convenient.
3. One-click Start
After the above "long and complex" configuration process, this step is simple.
How can I start it with one click? Everyone should have guessed -- startServer. bat -- yes. Double-click startServer. bat to run it. The console prompts you to have an http server service.
The default port number is "8080". You can modify it in "server. js", just in the first line of the Code.
4. Create multiple http servers
After talking about how to create the first http server for a long time, we have actually created the second, third, and fourth http server ...... They are all the same.
Test the three files in the demo1 folder to the location where you want to create the site. Open server. js, modify the port number and directory name, and run startServer. bat.
If you want to create an http server there, copy it to it and run it. If you don't want to use http server any more, simply delete the three files.
Easy to use!