node——express基本使用

首先需要下载 express 模块以及用于解析POST的 body-parser 模块

npm i express body-parser

express的基本配置

const app=require('express')();
const bodyParser=require('body-parser');

app.use(bodyParser.json());   //解析body中的json格式信息,必须添加body的解析规则
//app.use(bodyParser.raw()) - 解析二进制格式
//app.use(bodyParser.text()) - 解析文本格式
//app.use(bodyParser.urlencoded()) - 解析文本格式


app.use(bodyParser.urlencoded({
    extended:true
}));

app.get('/',function(req,res){
    console.log(req.query)
    res.send('GET')
});
app.post('/',function(req,res){
    res.send('POST')
    console.log(req.body)
});
app.listen(3001)
console.log('http://localhost:3001')

猜你喜欢

转载自blog.csdn.net/YUHUI01/article/details/83692962