nodejs--Web Server

Source: Internet
Author: User

The first article about Nodejs, the code here as a backup.

The code is not currently complete and is a blocking code.

1.index.js

Ar server=require ('./server '); var route=require ('./router '); var requesthandler=require ('./requesthandlers '); <!--declaring an object--  var handle={}handle['/']=requesthandler.start;handle['/start ']=requesthandler.start;handle['/upload ']=requesthandler.upload;server.start (route.route,handle) ;

2.server.js

varHttp=require (' http ');varUrl=require (' url ');functionStart (route,handle) {functionONrequest (request,response) {varPathname=Url.parse (request.url). Pathname; Console.log (' Received pathname: ' +pathname);                Route (Handle,pathname); Response.writehead (200,{' content-type ': ' Text/plain '}); Response.Write (' Hello World ');        Response.End (); } http.createserver (ONrequest). Listen (8888); Console.log (' Server has started ');} Exports.start=start;

3.route.js

function Route (handle,pathname) {        console.log (' about-route a request for ' +pathname);         if (typeof Handle[pathname] = = ' function ') {                handle[pathname] ();        } Else {                console.log (' No request handler found for: ' +pathname);}        } Exports.route=route;

4.requesthandlers.js

functionstart () {Console.log (' Request handler START was called '); functionSleep (millsec) {varStarttime=NewDate (). GetTime ();  while(NewDate (). GetTime () <starttime+millsec); } Sleep (10000); return' Hello Start ';}functionupload () {Console.log (' Request handler UPLOAD was called '); return' Hello Upload ';} Exports.start=Start;exports.upload=upload;

nodejs--Web 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.