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