【Express】获取请求参数

// index.js
const express = require('express');
const app = express();
const router = express.Router();

router.get('/', (req, res) => {
  // 获取数据 ?name='xxx'&age=18 -> { name: "'xxx'", age: '18' }
  console.log(req.query)
  res.send('get')
})
router.post('/', (req, res) => {
  // 获取数据 {"username":"xxx","password":123}  ->  [Object: null prototype] { username: 'xxx', password: '123' }
  console.log(req.body)
  res.send({ ok: 1 })
})
// 配置解析 post 参数的中间件 ?username='xxx'&password=123 形式
app.use(express.urlencoded({ extended: false }));
// json 形式
app.use(express.json());
app.use('/', router);

app.listen(3000, () => {
  console.log('Example app listening on port 3000!');
}); 

你可能感兴趣的:(Express,express,javascript,前端)