SyntaxError:意外的令牌节点js教程

时间:2015-08-14 04:44:47

标签: javascript node.js

所以我正在做这个教程https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4当我尝试启动服务器时,我收到此错误

/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

这就是我的代码的样子

// 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);

1 个答案:

答案 0 :(得分:3)

标记为84的行不是错误的位置。它位于该块之后调用的下一个函数中:

// 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);
        });
    });

你刚用分号完成了这个get()调用,但是你继续在预期的对象上调用下一个函数:

  // 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!' });
      });

  });
});

如果在.put(function(req, res){行之前删除分号,则不应出现语法错误。