我无法获取数据,Post函数运行正常,我正在使用Postman对其进行测试。当我放置localhost:3000/api/names/2
时,出现此错误
无法获取/ api / names / 2
const express = require('express');
const app = express();
var cors =require('cors')
app.use(express.json()); app.use(cors())
const names = []
app.get('/api/names/', (req, res) => {
res.send(names);
} );
app.post('/api/names', (req, res) => {
const name = {
id: names.length,
name :req.body.name
};
names.push(name);
res.send(name);
});
app.listen(3000, () => console.log('port 3000'));
答案 0 :(得分:1)
获取
app.get('/api/names/:id', (req, res) => { res.send(names.filter(x => x.id == req.params.id););