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 trial

JavaScript Build a REST API With Express Building API Routes in Express Building the Question Routes

req.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 can you take a look please. Thank you.

Steven Parker
Steven Parker
231,275 Points

Looks like Shaban beat me to it! :wink:

yeah. appreciate you coming tho.

1 Answer

Shaban Khawar
Shaban Khawar
1,831 Points

You are not using valid json but instead bson....