Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSaud Tauqeer
Courses Plus Student 8,099 Pointsreq.body not appearing in postman.
//app.js
const port = process.env.PORT || 3000;
const express = require ('express');
const app = express();
const jsonParser = require('body-parser').json;
const routes = require('./routes/routes.js');
app.use(jsonParser());
app.use("/questions", routes);
app.listen(port,(err)=>{
if (!err)
{console.log(`The express server is running at http://localhost:${port}`)}
});
//routes.js
const express = require('express');
const router = express.Router();
//GET /questions
//return all the questions from the db
router.get('/', function(req, res){
res.json({response: "You sent me a get request"});
});
//POST /questions
//Route for creating questions
router.post("/", function(req, res){
res.json({
response: "you sent me a POST request",
body: req.body
});
});
module.exports = router;
//The error is as following
SyntaxError: Unexpected token c in JSON at position 3
at JSON.parse (<anonymous>)
at parse (C:\Restful\node_modules\body-parser\lib\types\json.js:89:19)
at C:\Restful\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (C:\Restful\node_modules\raw-body\index.js:224:16)
at done (C:\Restful\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (C:\Restful\node_modules\raw-body\index.js:273:7)
at emitNone (events.js:106:13)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
Help appreciated can't figure out the problem.
Steven Parker
231,248 PointsLooks like Shaban beat me to it!
Saud Tauqeer
Courses Plus Student 8,099 Pointsyeah. appreciate you coming tho.
Saud Tauqeer
Courses Plus Student 8,099 PointsSaud Tauqeer
Courses Plus Student 8,099 PointsSteven Parker can you take a look please. Thank you.