我对这段代码有一个问题:
const express = require('express');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get('/', (req, res) => {
res.send([
{ id: 1, title: 'foo' }
]);
});
app.post('/', (req, res) => {
res.send({
data: req.body
})
console.log("Geht");
});
app.listen(3000, () => {
console.log('Server listening on port 3000.');
});
例如,如果我尝试使用"curl -X POST -d '{"user":"jane.doe"}‘-H "Content-Type: application/json“http://localhost:3000/”,我总是得到以下错误:
索引:在完成(C:\Programmieren\node test\node_modules\body-parser\lib\types\json.js:89:19) \node_modules\raw-body\ invokeCallback )时分析(C:\Programmieren\node test\node_modules\body-parser\lib\types\json.js:89:19) at C:\Programmieren\node test\node_modules\body-parser\lib\read.js:121:18 at invokeCallback (C:\Programmieren\node test\raw-body\index.js:224:16)时,JSON中位置1处的意外标记u.js:213:7)在IncomingMessage.onEnd (C:\Programmieren\node test\node_modules\raw-body\index.js:273:7)在IncomingMessage.emit (events.js:327:22)在endReadableNT (内部/streams/Readable.js:1327:12)在processTicksAndRejections (内部/进程/任务_队列:80:21)
有没有人能告诉我一个解决方案?
发布于 2021-07-25 14:17:45
对于windows用户,它可以对"
字符进行转义。
curl -X POST -d "{\"user\":\"jane.doe\"}" -H "Content-Type: application/json" http://localhost:3000/
https://stackoverflow.com/questions/68519226
复制