express nodejs Web应用开发框架
安装
- npm install express ejs body-parser --save
使用
let express = require('express');
let app = express();
app.listen(3000,()=>{
console.log(`服务启动在3000端口`)
})
app.listen = (...args)=>{
require('http').createServer(this).listen(...args)
}
复制代码
主要功能:
- 路由控制
- 参数获取(req扩展的方法)
- 中间件
- send 和sendFile(res扩展的方法)
- 静态文件服务
- 模版解析
- 重定向
主要功能分析
let express = require('express');
let app = express();
app.listen(3000,()=>{console.log(`服务运行在3000端口`)})
app.get('/signin',(req,res)=>{
res.setHeader('Content-Type','text/plain;charset=utf8')
res.end('登录')
})
app.post('/signup',(req,res)=>{
res.setHeader('Content-Type','text/plain;charset=utf8');
res.end(post注册)
})
app.all('*',(req,res)=>{
res.end(404)
})
复制代码
let express = require('express')
let app = express();
app.listen(3000)
app.get('/user',(req,res)=>{
console.log(req.query.id)
console.log(req.url)
console.log(req.path)
console.log(req.headers)
console.log(req.method)
if(req.query.id){
}else{
}
})
--------
let express = require('express');
let app = express();
app.listen(3000)
app.get('/user',(req,res)=>{
res.end('select All')
})
app.get('/user/:id/:name',(req,res)=>{
console.log(res.params)
res.end(`select one ${req.params.id}${req.params.name}`)
})
let url = '/user/1/xiaoming';
let path = '/user/:id/:name';
let keys = []
let newPath = path.replace(/:([^\/]+)/g,function(...args){
keys.push(args[1])
return '([^\/]+)';
})
let reg = new RegExp(newPath);
let values = reg.exec(url);
let params = {};
keys.forEach((key,index)=>{
params[key] = values[index+1]
})
console.log(params)
---------
let express = require('express')
let app =express();
app.listen(3000)
app.param('id',(req,res,next)=>{
req.params.id = `你的学号是${req.params.id}`
next();
})
app.param('name',(req,res,next)=>{
req.params.name = '你的名字是${req.params.name}'
next()
})
app.get('/user/:id/:name',(req,res)=>{
res.header('Content-Type','text/plain;charset=utf-8')
res.end(req.params.id + req.params.name)
})
复制代码
let express =require('express');
let app = express();
app.listen(3000);
app.use('/water',(req,res,next)=>{
console.log('过滤石头');
req.stone = 'too big'
next();
})
app.use('/water',function(req,res,next)=>{
console.log('过滤沙子');
req.sand = ' too small'
next();
})
app.use((req,res,next)=>{
res.setHeader('Content-Type','text/plain;charset=utf8');
next()
})
app.get('/water',(req,res)=>{
console.log('过滤完成')
console.log(req.stone,req.sand)
res.end('水')
})
app.get('/water/a',(req,res)=>{
console.log('过滤完成')
console.log(req.stone,req.sand)
res.end('水')
})
app.get('/food',function (req, res) {
console.log(req.stone,req.sand)
res.end('食物')
})
app.use((err,req,res,next)=>{
console.log(err)
})
复制代码
let express = require('express');
let app = express();
app.listen(3000)
app.use('/eat',(req,res,next)=>{
let t = new Date().getTime();
let end = res.end;
res.end = (...args)=>{
end.call(res,...args);
console.log(new Date().getTime() - t)
}
})
app.get('/eat/water',(req,res)=>{
for(let i = 0 ;i<10000;i++){
}
res.end('water')
})
app.get('/eat/food',(req,res)=>{
for(let i = 0 ;i<10000000;i++){
}
res.end('food')
})
复制代码
- js模拟express 中间件 next方法思路:
function app (){
}
//每次调用use方法,都会将方法存到数组中;
app.middleWares = []
app.use = function(cb){
this.middleWares.push(cb)
}
app.use((req,res,next)=>{
console.log(1)
next()
})
app.use((req,res,next)=>{
console.log(2)
next()
})
app.use((req,res,next)=>{
console.log(3)
})
//默认调用数组第一项,将next方法传递给数组中的方法;如果调用next 会继续执行数组中的下一项
let i = 0;
function next(){
app.middleWares[i](null,null,next);
i++;
}
next();
复制代码
- res 扩展的方法 send sendFile json sendStatus
let express = require('express');
let app = express();
app.listen(3000);
app.get('/json',(req,res)=>{
res.json({name:'小明'})
})
app.get('/',(req,res)=>{
res.sendFile('./index.html',{root:__dirname});
res.sendFile(require('path').join(__dirname,'..','index.html'))
})
app.get('/status',(req,res)=>{
res.sendStatus(200)
});
app.use(function(req,res,next){
req.mySend = function(data){
if(typeof data =='object'){
res.setHeader('Content-Type','application/json;charset=utf8')
res.end(JSON.stringify(data))
return
}
if(typeof data =='string'){
res.setHeader('Content-Type','text/plain;charset=utf8')
res.end(data)
return
}
if(typeof data = 'number'){
res.statusCode = data;
let _http_server = require('_http_server');
res.end(_http_server.STATUS_CODES[data])
return
}
next()
}
})
app.get('/send',(req,res)=>{
res.mySend(404)
})
复制代码