Nodejs from HelloWorld to high-quality backend service server

Source: Internet
Author: User
Tags nginx reverse proxy

---restore content starts---

The new company uses the Nodejs as the app and the website backstage service server, so recently to Nodejs has been studying, plus before simple study a bit, looked at two days after the interface source code, therefore directly to get started to work, below is me and writes the background interface Brother's dialogue:

Backstage: After two days of backstage management you write, write with express or not?

I: After two days of the source code, I think with express more effort, because to involve some interface operation, with Express to create Project Application template and some other tools more convenient, but from the assignment, with their own written server code structure clearer, the front and back end completely separated, Non-impact.

Backstage: Do you know about restful?

I: I know a little bit, the interview was asked. (The interview was really asked, went back after Baidu, but forgot)

Then the backstage brother explained to me some things that are related to the restful, but I'm a little too clear.

In background API server written by brother behind the scenes, according to the most basic server agency written, on the code:

Server.js

varHttp=require (' http ');varRouter=require ('./router ')), URL=require (' url '), ServerPort=8888;varLogger=require ('./common ')). Logger;functionStart (router,handler) {functionONrequest (request,response) {request.setencoding (' UTF-8 '); varPostdata= ""; Request.addlistener (' Data ',function(postdatachunk) {postdata+=Postdatachunk;                }); Request.addlistener (' End ',function() {Logger.info (' Server is normal,request recieved ... ');        Router (request,response,postdata,handler);            });    } http.createserver (ONrequest). Listen (ServerPort); Logger.info (' Server start on port: ' +serverport);} Logger.trace (' Entering Cheese testing '); Exports.start= start;

Router.js:

var url=require (' url '), var fs=require (' FS '), function router (request,response,postdata,handler) {var pathname= Url.parse (Request.url). Pathname;console.log (' request received at ' +pathname); Console.log (' pathname: ' +pathname); Console.log (' handler[pathname]: ' +handler[pathname]); if (typeof handler[pathname] = = = ' function ') {Handler[pathname] (Request,response,postdata);} Else{console.log (' Read resource here ..., now reading resources are: >>> ' +pathname); Fs.readfile (__dirname+ Pathname,function (Err,data) {if (err) {response.writehead (404,{' content-type ': ' text/html '}) Response.Write (' < Center>

  

Requesthandlers.js:

var logger = require ('./common '). Logger;var util=require (' util '); function Response_err (response,err) {var rdata={}; rdata[' result ']= '-1 '; rdata[' message ']=err;response.write (json.stringify (rdata)); Response.End (); if (Err.code = = = ') Protocol_enqueue_after_fatal_error ') {    process.exit (0);    }        Logger.info (' <<< response_err ', json.stringify (Rdata));} function Response_success (response,rdata) {rdata[' result ']= ' 0 '; Response.Write (Json.stringify (rdata)); Response.End (); Logger.info (' <<< response_success. ', Json.stringify (Rdata));} function Login (request,response,postdata) {Redirect_login (Request,response,postdata,function (err,rdata) {if (err) { Response_err (Response,err);} Else{response_success (Response,rdata);}});} function Qzone (request,response,postdata) {function teacherlist (request,response,postdata) {redirect_teacherlist ( Request,response,postdata);} Exports.index=redirect_index;exports.login=login;

Index.js:

var http= require (' http '), Router=require ('./router '), Requesthandlers=require ('./requesthandlers '), Server=require ('./server '), Cluster=require (' cluster '), Cpunums=require (' OS '). CPUs (). Length;var handlers={};handlers['/oper/ Login ']=requesthandlers.login;if (cluster.ismaster) {console.log (' Master work start ... '); for (Var i=0;i<cpunums;i + +) {cluster.fork ();} Cluster.on (' Exit ', function (worker,code,signal) {console.log (' worker ' +worker.process.pid + ' died ... ');}); Else{server.start (router.router,handlers);}

  

Are excerpts of the code, some local development, in order to avoid leaking work, the excerpt.

Structure is such a structure, and then slowly add the required modules, combined with Nginx reverse proxy use, very OK.

Nodejs from HelloWorld to high-quality backend service 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.