声明:在写nodejs代码的时候,很多模块可以封装保存起来,以后的项目都会用到。

1、路由模块

var url=require('url');

//封装方法改变res  绑定res.send()
function changeRes(res){
res.send=function(data){
res.writeHead(,{"Content-Type":"text/html;charset='utf-8'"});
res.end(data);
}
} //暴露的模块
var Server=function(){
var G=this; /*全局变量*/
//处理get和post请求
this._get={};
this._post={};
var app=function(req,res){
changeRes(res);
//获取路由
var pathname=url.parse(req.url).pathname;
if(!pathname.endsWith('/')){
pathname=pathname+'/';
}
//获取请求的方式 get post
var method=req.method.toLowerCase();
if(G['_'+method][pathname]){
if(method=='post'){ /*执行post请求*/
var postStr='';
req.on('data',function(chunk){
postStr+=chunk;
})
req.on('end',function(err,chunk) {
req.body=postStr; /*表示拿到post的值*/
G['_'+method][pathname](req,res); /*执行方法*/
})
}else{ /*执行get请求*/
G['_'+method][pathname](req,res); /*执行方法*/
}
}else{
res.end('no router');
}
} app.get=function(string,callback){
if(!string.endsWith('/')){
string=string+'/';
}
if(!string.startsWith('/')){
string='/'+string;
}
// /login/
G._get[string]=callback;
} app.post=function(string,callback){
if(!string.endsWith('/')){
string=string+'/';
}
if(!string.startsWith('/')){
string='/'+string;
}
G._post[string]=callback;
}
return app;
}
module.exports=Server();

2、直接摘抄,然后创建一个文件运行下面的代码

方式一:
    var http = require('http');
    var app = require('./express-route');
    http.createServer(app).listen('');     app.get('login',function (req,res) {
    console.log('login');
    res.end('login');
    })     app.get('register',function (req,res) {
    console.log('register');
    res.end('register');
    }) 方式二:
    var http = require('http');
    var url = require('url');
    var path = require('path');
    var app = require('./module/express-route');
    var ejs = require('ejs');
    http.createServer(app).listen('');     app.get('/',function (req,res) {
    var msg = 'route test';
    ejs.renderFile('练习/views/index.ejs',{msg:msg},function (err,data) {
    if(err){
    console.log(err);
    return false;
    }
    console.log(data);
     //服务器端有数据返回到客户端 这个时候必须用res.send()。
    //如果没有数据返回到客户端,res.send()和res.end()效果一样。
         res.send(data);
        })
    })

最新文章

  1. EL表达式杂项
  2. 如何删除datatable中的一行数据
  3. 面试问题4:C语言预处理包括哪些
  4. Hadoop 学习笔记3 Develping MapReduce
  5. Anagrams问题
  6. 如何获取版本的 Internet 信息服务器 (IIS)
  7. 关于 .crash 分析
  8. 使用psftp.exe
  9. 剑指Offer_4_二维数组中的查找
  10. IIC时序操作24C02芯片
  11. 字符串API练习三则
  12. 20155205 郝博雅 《网络对抗技术》Exp1 PC平台逆向破解
  13. Vue-发布订阅机制(bus)实现非父子组件的传值
  14. python全栈开发day49-jquery的位置信息、事件流、事件对象,事件委托,事件绑定和解绑
  15. 雷林鹏分享:XML 属性
  16. scrapy 常用代码
  17. VS2013 生成事件,删除不必要的DLL
  18. curl dns缓存设置
  19. 正则表达式(三):Unicode诸问题下篇(转)
  20. 解决Notepad++ Plugin Manager无法加载插件的方法

热门文章

  1. deep_learning_tensorflow_get_variable()
  2. 1.SpringBoot整合Mybatis(CRUD的实现)
  3. oracle监听启动很慢
  4. java8学习之groupingBy源码分析
  5. Django学习系列12:把Python变量传入模板中渲染
  6. 【CF160E】Buses and People
  7. thinkphp5权限仿制
  8. 【Linux学习一】命令行CLI、BASH的基本操作
  9. JavaScript基础——JavaScript常量和变量(笔记)
  10. LINUX装机问题:无法使用“Ctrl+Alt+[F1~F6]”快捷键切换到终端