【发布时间】:2021-03-03 14:14:19
【问题描述】:
好吧,我只是想构建一个简单的 expressJS 应用程序,但似乎没有任何效果,甚至这段代码也不行:
const express = require('express');
const bodyParser = require('body-parser');
require('dotenv').config();
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
/* require('./src/routes/index')(app); */
app.post('/', async (req, res) => {
try {
res.send(req);
} catch(e) {
console.log(e);
res.send({ error: e.message });
}
});
app.listen(process.env.PORT, () => {
console.log(`Server initialized. Try it on http://localhost:${process.env.PORT}`);
})
每次我尝试发布这个简单的 JSON 请求时:
{
"name": "namesample",
"email": "emailsample",
"password": "passwordsample"
}
我收到此错误:
{
"error": "Converting circular structure to JSON\n --> starting at object with constructor 'Socket'\n | property 'parser' -> object with constructor 'HTTPParser'\n --- property 'socket' closes the circle"
}
console.log 打印:
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Socket'
| property 'parser' -> object with constructor 'HTTPParser'
--- property 'socket' closes the circle
at JSON.stringify (<anonymous>)
at stringify (C:\Users\joaov\Documents\Github Projects\jwt-study\node_modules\express\lib\response.js:1123:12)
at ServerResponse.json (C:\Users\joaov\Documents\Github Projects\jwt-study\node_modules\express\lib\response.js:260:14)
at ServerResponse.send (C:\Users\joaov\Documents\Github Projects\jwt-study\node_modules\express\lib\response.js:158:21)
at C:\Users\joaov\Documents\Github Projects\jwt-study\app.js:14:13
at Layer.handle [as handle_request] (C:\Users\joaov\Documents\Github Projects\jwt-study\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\joaov\Documents\Github Projects\jwt-study\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\joaov\Documents\Github Projects\jwt-study\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\joaov\Documents\Github Projects\jwt-study\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\joaov\Documents\Github Projects\jwt-study\node_modules\express\lib\router\index.js:281:22
有人可以帮忙吗?我的代码无法识别我的请求!!!
谢谢
【问题讨论】: