Use Nodejs to build the simplest and lightest HTTP server

Source: Internet
Author: User
Tags node server

1. Introduction

Front-end program apes are primarily concerned with the page, and you may not be able to use it at all. net,java,php and other background languages.

But the pages you make are always run, always tested. --that's the inevitable use of HTTP server. I used to use Visual Studio, although it is very useful, very powerful, but I developed a HTML, JavaScript, CSS, why use this silly big coarse things. Open once particularly slow, account for the memory is particularly strong, the installation of C disk to take up several G space.

Later, there was nothing to replace the Nodejs. Instead of installing any plug-ins, you only need to manually create three small files (2KB in total), run and fast. The feeling is really very simple and light weight. Share, know please skip, do not like to spray!

2. Four-step setup

The first step is to install Nodejs:

Not much nonsense, to www.nodejs.org download installation can.

Step two, create two JS files

Create two JS files, each saved as Server.js and Mine.js, with the following code:

 1 var PORT = 8080;     Port 2 var DIR = ' test1 '; Directory for storing HTML 3 4 var http = require (' http '); 5 var url=require (' url '); 6 var fs=require (' FS '); 7 var mine=require ('./mine '). Types; 8 var path=require (' path ');      9 var server = Http.createserver (function (request, response) {One-by-one var pathname = Url.parse (request.url). pathname;12     var Realpath = Path.join (DIR, pathname);//console.log (Realpath); var ext = path.extname (realpath); 15 Ext = ext? Ext.slice (1): ' Unknown '; fs.exists (Realpath, function (exists) {+ if (!exists) {RESPONSE.WR Itehead (404, {content-type ': ' Text/plain '), Response.Write ("This requ EST URL "+ pathname +" is not found on the this server. ");                 Response.End (), or else {fs.readfile (Realpath, "binary", function (err, file) {26 if (err) {Response.writehead (Ntent-type ': ' Text/plain '); Response.End (err);} else {32 var contentType = Mine[ext] | |                     "Text/plain", Response.writehead ($, {content-type '): contentType35                 }); Response.Write (file, "binary"); PNs Response.End (); 38 }39}),}41}), Server.listen (port), Console.log ("Server runing at PORT:" + P ORT + ".");
1 exports.types = {2   "CSS": "Text/css", 3   "gif": "Image/gif", 4   "html": "Text/html", 5   "ico": "Image/x-i Con ", 6   " JPEG ":" Image/jpeg ", 7   " jpg ":" Image/jpeg ", 8   " JS ":" Text/javascript ", 9   " JSON ":" Application /json ","   PDF ":" Application/pdf ","   png ":" Image/png ",   " svg ":" Image/svg+xml ",   " swf ":" Application/x-shockwave-flash ","   TIFF ":" Image/tiff ","   txt ":" Text/plain ","   wav ":" audio/ X-wav ","   wma ":" Audio/x-ms-wma ","   wmv ":" Video/x-ms-wmv ","   xml ":" Text/xml "20};

I create a new "Demo1" folder on the desktop and copy the two created JS files into it.

Step three, create a. bat command file

or the "Demo1" folder, and then create a "startserver.bat" file in this folder, the contents of the file is actually a word:

Node Server.js

At this point, there are three files in the Demo1 folder:

Fourth step, add your HTML file

or the "Demo1" folder, we need to create a "test1" folder in this file, and then we'll add a index.html and wait for the test.

The key point to note: Why is there a "test1" folder? Because the directory configured in our "Server.js" is "test1". If you look uncomfortable, you can get rid of it, but make sure the two names are the same.

If we have several test items in the Demo1, which one we need to run, it is very convenient to turn on "server.js" to modify the configuration.

3. One-click Start

After the "lengthy and complex" configuration process above, this step is simple.

How do I start with "one click"? Everyone should be able to guess.--startserver.bat--yes, double-click Run Startserver.bat and the console will prompt you for the HTTP Server service.

The port number defaults to "8080" and can be modified in "Server.js", just in the first line of code.

4. Create multiple HTTP servers

We talked about it for a long while, and we talked about how to create the first HTTP server, actually creating a second, third, fourth ... is as simple as that.

Take the three files in the Demo1 folder and test the location where you want to create the site. Open Server.js Modify the port number and directory name, run Startserver.bat.

If you want to create an HTTP server there, copy to where, and then run. Do not want to use the HTTP server again, directly delete the three files on the line.

Easy to use!

" from design to model"" deep understanding of JavaScript prototype and closure series " "Microsoft petshop4.0 Source Interpretation Video" "Json2.js Source Interpretation video"

Article Source: JS note Note (14)--use Nodejs to build the simplest and lightest HTTP server

Use Nodejs to build the simplest and lightest HTTP server

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.