2015-08-14 40 views
1

所以我做这个教程https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4当我当我尝试启动服务器我得到这个错误语法错误:意外的令牌节点JS教程

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

无论你用什么来编辑你的代码,都应该支持某种内置的“linting”(验证你的语法),它会突出显示这些容易犯的错误。 你甚至可以使用免费的在线工具,如[jsFiddle](http://jsfiddle.net),[CodePen](http://codepen.io)等,把你的JS,“整理”它是一致的格式和可读性,并突出显示任何错误。这里是[CodePen中的代码](http://codepen.io/greglockwood/pen/yNrqYO?editors=001),它向您显示右下方的一个小错误图标,您可以点击查看违规行。 – GregL

回答

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){行之前删除分号,则不应该有语法错误。

+1

此外,我猜''行:84 - >>>“字符串也不应该在那里。它最终会尝试对一个字符串调用'.get()',这很可能不会起作用(但它直到运行时才能知道)。 – GregL

+0

你说的没错,GregL - 我认为OP刚把它放在那里,以便在源代码中标出行号。 –

+0

是的,那是点84线是 – Hmgaar