Reputation: 51
So I am doing this tutorial https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4 and when I when i try to start server I get this error
/server.js:84
.put(function(req, res) {
^
SyntaxError: Unexpected token .
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
and this is how my code looks like
// server.js
var router = express.Router(); // get an instance of the express Router
// middleware to use for all requests
router.use(function(req, res, next){
// do logging
console.log('For every request this message is being shown.');
next(); // make sure we go to the next routes and don't stop here
});
// test route to make sure everything is working
router.get('/', function(req, res){
res.json({ message: 'Whatsup Welcome to my first node api!'});
});
// more routes for our API will continue here
// on routes that end in /Bears
router.route('/bears')
// create a bear (accessed at POST http://localhost:8080/bears)
.post(function(req, res) {
var bear = new Bear(); // create a new instance of the Bear model
bear.name = req.body.name; // set the bears name (comes from the request)
bear.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Bear has been born!' });
});
})
// get all the bears (accessed at GET http://localhost:8080/api/bears)
.get(function(req, res) {
Bear.find(function(err, bears) {
if (err)
res.send(err);
res.json(bears);
});
});
// on routes that end in /bears/:bear_id
// ------------------------------------------------
router.route('/bears/:bear_id')
// get all the bears (accessed at GET http://localhost:8080/api/bears/:bear_id)
"line :84 ->>>" .get(function(req, res) {
Bear.find(function(err, bears) {
if (err)
res.send(err);
res.json(bears);
});
});
// update the bear with this id (accessed at PUT http://localhost:8080/api/bears/:bear_id)
.put(function(req, res) {
// use our bear model to find the bear we want
Bear.findById(req.params.bear_id, function(err, bear) {
if (err)
res.send(err);
bear.name = req.body.name; // update the bears info
// save the bear
bear.save(function(err) {
if (err)
res.send(err); }
res.json({ message: 'Bear updated!' });
});
});
});
// REGISTER OUR ROUTES here
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE server
app.listen(port);
console.log('Magic is happening on port:' + port);
Upvotes: 1
Views: 3550
Reputation: 7700
The line marked as 84 is not where the error is. It's in the next function you call after that block:
// on routes that end in /bears/:bear_id
// ------------------------------------------------
router.route('/bears/:bear_id')
// get all the bears (accessed at GET http://localhost:8080/api/bears/:bear_id)
"line :84 ->>>" .get(function(req, res) {
Bear.find(function(err, bears) {
if (err)
res.send(err);
res.json(bears);
});
});
You just finished this get()
call with a semicolon, but you then go on to call the next function on an expected object:
// update the bear with this id (accessed at PUT http://localhost:8080/api/bears/:bear_id)
.put(function(req, res) {
// use our bear model to find the bear we want
Bear.findById(req.params.bear_id, function(err, bear) {
if (err)
res.send(err);
bear.name = req.body.name; // update the bears info
// save the bear
bear.save(function(err) {
if (err)
res.send(err); }
res.json({ message: 'Bear updated!' });
});
});
});
If you remove the semicolon just before the .put(function(req, res){
line, it should not have a syntax error.
Upvotes: 3