3 min Quick Build Nodejs local server run Test Html/js (GO)

Source: Internet
Author: User

1, to Nodejs official website Download the installation package http://nodejs.cn/automatically maps the environment to the system after the installation is complete, does not need to configure the environment variable, then opens the console
Input Command: node
No error indicates successful operation
2. Next, under the test directory sibling directory, create the following two JS files
Http.js (Server script configuration file)

varPORT = 3000;//varHTTP = require (' http ');varUrl=require (' url ');varFs=require (' FS ');varMine=require ('./mine '). Types;//varPath=require (' Path ');varServer = Http.createserver (function(Request, response) {varpathname =Url.parse (request.url). Pathname; varRealpath = Path.join ("WebApp", pathname);//set your own file name here;    varext =Path.extname (Realpath); Ext= ext? Ext.slice (1): ' Unknown '; Fs.exists (Realpath,function(exists) {if(!exists) {Response.writehead (404, {                ' Content-type ': ' Text/plain '            }); Response.Write ("This request URL" + pathname + "is not found on this server.");        Response.End (); } Else{fs.readfile (Realpath,"Binary",function(err, file) {if(Err) {Response.writehead (500, {                        ' Content-type ': ' Text/plain '                    });                Response.End (ERR); } Else {                    varContentType = Mine[ext] | | "Text/plain"; Response.writehead (200, {                        ' Content-type ': ContentType}); Response.Write (File,"Binary");                Response.End ();        }            }); }    });}); Server.listen (PORT); Console.log ("Server runing at Port:" + Port + ".");

Mine.js (introducing the corresponding file)

Exports.types = {  "CSS": "Text/css",  "GIF": "Image/gif",  "HTML": "Text/html",  "ico": "Image/x-icon",  "JPEG": "Image/jpeg",  "JPG": "Image/jpeg",  "JS": "Text/javascript",  "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"};

Above two JS new successful, open http.js, and then found in the path settings, the current WebApp to your own name of the project name (if it is with JS in the same directory. If the test project is in another file, then the path may have to be set to the absolute path _ this I did not try)
Below is the file that corresponds to my directory, three arrows are required other without setting

3. The console starts the server test is successful
See:
Open the directory where the project is entered node Http.js
Then prompt the port to start successfully,
Then enter the address of the file you want to test directly. Like mine.
Http://localhost:3000/index.html

All of this deployment is complete!

Note here: Be sure to add your own file path after the port number to run successfully. Otherwise it will be an error because the http.js is relatively simple to write, not directly mapped
http://localhost:3000
If something goes wrong, then rewrite the startup service
That is: node. js
Of course, if you're interested, you can rewrite node. js to refine it

3 minute Quick build Nodejs local server run Test Html/js (GO)

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.