当前位置: 首页 > 后端技术 > Node.js

使用express连接http

时间:2023-04-03 23:02:33 Node.js

constexpress=require("express");//创建http实例constapp=express();app.use((req,res,next)=>{console.log('requeststart...',req.method,req.url);next();})app.use((req,res,next)=>{req.cookie={userId:'aaa111'}next()})app.use('/api',(req,res,next)=>{console.log('Processing/apirouting');next();})app.get('/api',(req,res,next)=>{console.log('getprocessing/apirouting');next();})app.post('/api',(req,res,next)=>{console.log('postHandle/apiroute');next();});app.use((req,res,next)=>{console.log('handle404')res.json({errno:-1,msg:'404notfount'})})app.listen(3000,()=>{console.log('服务器在端口3000上运行')})