标签:
---恢复内容开始---
新公司用的nodejs作为app和网站的后台服务server,所以最近对nodejs一直在学习,加上之前简单的学习了一点,看了两天后台接口源码,所以就直接上手干活了,下面是我和写后台接口的哥哥的一段对话:
后台:过两天的后台管理你写的话,用express写还是不用?
我 :看了两天后台源码,我觉得用express更省力一些,因为要牵涉到一些界面操作,用express创建项目应用模板和其他一些工具更方便,但是从工作分配上来说的话,用自己写的server代码结构更清晰,前后端完全分离,互不影响。
后台:了解RESTful吗?
我 :了解过一点,面试被问到过。(面试确实被问到过,回去之后百度了下,但是忘了)
然后后台哥哥给我解释了下RESTful相关的一些东西,但是我有点不是太明白。
在后台哥哥写的后台api server中,按照最基本的server机构写的,上代码:
server.js
var http=require(‘http‘); var router=require(‘./router‘), url=require(‘url‘), serverPort=8888; var logger=require(‘./common‘).logger; function start(router,handler){ function onRequest(request,response){ request.setEncoding(‘UTF-8‘); var postData=""; 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 is : >>> ‘+pathname); fs.readFile(__dirname+pathname,function(err,data){ if(err){ response.writeHead(404,{‘content-type‘:‘text/html‘}) response.write(‘<center><h1 style="font-family:microsoft yahei">‘); response.write(‘404,Sorry ,not found your visiting page...‘); response.write(‘</h1></center>‘) response.end(); } else{ response.write(data); response.end(); } }); } } exports.router=router;
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); }
都是节选代码,有的地方式开发用的,为了避免泄露工作方面的,就节选了。
结构是这么个结构,然后再慢慢添加需要的模块,结合nginx的反向代理使用,很ok。
nodejs 从helloworld到高质量的后台服务server的一点思考
标签:
原文地址:http://www.cnblogs.com/greensoon/p/4424624.html