我对 Rest APIs 这个主题很陌生,我正在创建一个简单的主题,在其中接收包含电影数据的 json 并给出一个小响应。由于我是新手,我想从逻辑开始,它是一个保存数据的数组。
但是当我通过 PostMan 发送数据时,它会很好地保存所有内容并完成其工作,但我在控制台上收到此错误。
请有人能指导我
我的代码
router.post('/', (req, res) => {
const {titulo, director, year, rating} = req.body;
if (titulo && director && year && rating) {
const id = movies.length + 1;
const newMovie = {...req.body, id};
movies.push(newMovie);
res.json(movies);
} else {
res.status(500).json({error: 'Ha Ocurrido un error'});
}
res.send('recibido');
});
module.exports = router;
Run Code Online (Sandbox Code Playgroud)
命令错误
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:371:5)
at ServerResponse.setHeader (node:_http_outgoing:576:11)
at …Run Code Online (Sandbox Code Playgroud)