Reputation: 926
Well, I'm just trying to build a simple expressJS app, but nothing seems to work, not even this code:
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}`);
})
Every time I try to POST this simple JSON request:
{
"name": "namesample",
"email": "emailsample",
"password": "passwordsample"
}
I get this error:
{
"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"
}
and the console.log prints this:
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
Could anyone help with that please? My code isn't recognizing my request!!!
Thanks
Upvotes: 1
Views: 7721
Reputation: 261
if you just wanted to check your post request working or not you can change
res.send(req);
to res.send(req.body);
if you want to see the entire request object itself install circular-json node module and you can make changes as follows.
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}`);
})
Upvotes: 4