Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js NodeJS Express-TypeError:将循环结构转换为JSON_Node.js_Json_Express - Fatal编程技术网

Node.js NodeJS Express-TypeError:将循环结构转换为JSON

Node.js NodeJS Express-TypeError:将循环结构转换为JSON,node.js,json,express,Node.js,Json,Express,嗯,我只是尝试构建一个简单的expressJS应用程序,但似乎没有任何效果,即使是以下代码: const express=require('express'); const bodyParser=require('body-parser'); require('dotenv').config(); 常量app=express(); use(bodyParser.json()); use(bodyParser.urlencoded({extended:true})); /*要求('./src/ro

嗯,我只是尝试构建一个简单的expressJS应用程序,但似乎没有任何效果,即使是以下代码:

const express=require('express');
const bodyParser=require('body-parser');
require('dotenv').config();
常量app=express();
use(bodyParser.json());
use(bodyParser.urlencoded({extended:true}));
/*要求('./src/routes/index')(应用程序)*/
app.post('/',异步(req,res)=>{
试一试{
res.send(req);
}捕获(e){
控制台日志(e);
res.send({错误:e.message});
}
});
app.listen(process.env.PORT,()=>{
console.log(`Server initializedhttp://localhost:${process.env.PORT}`);
})
每次我尝试发布这个简单的JSON请求时:

{
“名称”:“名称样本”,
“电子邮件”:“电子邮件样本”,
“密码”:“passwordsample”
}
我得到这个错误:

{
“错误”:“将循环结构转换为JSON\n-->从具有构造函数'Socket'的对象开始\n |属性'parser'->具有构造函数'HTTPParser'的对象\n---属性'Socket'关闭循环”
}
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
TypeError:将循环结构转换为JSON
-->从具有构造函数“Socket”的对象开始
|属性“parser”->具有构造函数“HTTPParser”的对象
---属性“socket”关闭圆
在JSON.stringify()上
在stringify(C:\Users\joaov\Documents\Github Projects\jwt study\node\u modules\express\lib\response.js:1123:12)
在ServerResponse.json(C:\Users\joaov\Documents\Github Projects\jwt study\node\u modules\express\lib\response.js:260:14)
在ServerResponse.send(C:\Users\joaov\Documents\Github Projects\jwt study\node\u modules\express\lib\response.js:158:21)
在C:\Users\joaov\Documents\Github Projects\jwt study\app.js:14:13
在Layer.handle[作为handle\u请求](C:\Users\joaov\Documents\Github Projects\jwt study\node\u modules\express\lib\router\Layer.js:95:5)
接下来(C:\Users\joaov\Documents\Github Projects\jwt study\node\u modules\express\lib\router\route.js:137:13)
在Route.dispatch(C:\Users\joaov\Documents\Github Projects\jwt study\node\u modules\express\lib\router\Route.js:112:3)
在Layer.handle[作为handle\u请求](C:\Users\joaov\Documents\Github Projects\jwt study\node\u modules\express\lib\router\Layer.js:95:5)
在C:\Users\joaov\Documents\Github Projects\jwt study\node\u modules\express\lib\router\index.js:281:22
有人能帮忙吗?我的代码无法识别我的请求


谢谢

如果您只是想检查您的post请求是否有效,您可以更改
res.send(请求)发送至
res.send(请求正文)

如果您想看到整个请求对象本身,请安装循环json节点模块,您可以进行如下更改

const express = require('express');
const bodyParser = require('body-parser');
const circularJSON = require('circular-json');
require('dotenv').config();

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/', async (req, res) => {
    try {
        let json = circularJSON.stringify(req);
        res.send(json);
    } 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}`);
})

这实际上是一个非常好的答案,有额外的信息!Idk为什么,req.body也不起作用,但现在它开始正常工作了:D关于循环的东西很有用,顺便说一句,谢谢男士:D