Build the simplest and lightweight http server with nodejs

Source: Internet
Author: User

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!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.