中间件
var http = require('http');
var url = require('url');
var query = requires('./query');
var server = http.createServer(function(req,res){
query(req);
});
server.listener(3000,function(){
console.log('3000,running...');
});
module.exports = function(req){
req.query = {}
}
var query = requires('./query');
express中间件
var express = require('express')
var app = express()
app.use(function(req,res,next){
console.log('1');
})
app.use(function(req,res,next){
console.log('2');
})
app.use('/a',function(req,res,next){
console.log(req.url);
})
app.use('/b',function(req,res,next){
console.log(req.url)
})
app.get('/',function(req,res,next){
console.log('/');
})
app.get('/a',function(req,res,next){
console.log('/a')'
})
app.listen(3000,function(){
console.log('running...');
})
配置一个全局处理中间件
app.get('/a',function(req,res,next){
fs.readFile('xxx/xxx',function(err,data){
if(err){
return next(err)
}
})
})
app.use(function(req,res,next){
res.render('404.html');
})
app.use(function(err,req,res,next){
res.status(500).json({
err_code:500,
message:err.message
})
})