关于node.js:使用express连接http

42次阅读

共计 550 个字符,预计需要花费 2 分钟才能阅读完成。

const express = require("express");
 
// 创立 http 实例
const app = express();
 
app.use((req,res,next) => {console.log('申请开始...', req.method,req.url);
   next();})
 
app.use((req,res,next) => {
   req.cookie = {userId: 'aaa111'}
   next()})
 
app.use('/api', (req,res,next) => {console.log('解决 /api 路由');
   next();})
app.get('/api', (req,res,next) => {console.log('get 解决 /api 路由');
   next();})
app.post('/api', (req,res,next) => {console.log('post 解决 /api 路由');
   next();});
 
app.use((req, res, next) => {console.log('解决 404')
    res.json({
        errno: -1,
        msg: '404 not fount'
    })
})
 
app.listen(3000, () => {console.log('server is running on port 3000')
})

正文完
 0