2017-05-08 47 views
0

我的代碼如下所示:無法讀取的 'GET' 在快遞路由器未定義的屬性

app.js

const app = express(); 
const itemsRouter = require('./routes/items.js'); 

app.use(bodyParser.urlencoded({ 
    extended: true 
})); 

app.use('/items', itemsRouter); 

items.js

const router = require('express').router; 

router.get('/itemTest', (req, res) => { 
    res.json({ 
     sucess: true 
    }); 

    console.log(`the parametrs are ${req.body.item_name} ${req.body.item_post}`); 
}); 

module.exports = router; 

但在這裏不知何故,我無法使路由器工作,並且它說無法讀取未定義的屬性'get'。

錯誤堆棧如下圖所示:

TypeError: Cannot read property 'get' of undefined 
    at Object.<anonymous> (C:\Users\anand\quFlipApi\routes\items.js:5:7) 
    at Module._compile (module.js:570:32) 
    at Object.Module._extensions..js (module.js:579:10) 
    at Module.load (module.js:487:32) 
    at tryModuleLoad (module.js:446:12) 
    at Function.Module._load (module.js:438:3) 
    at Module.require (module.js:497:17) 
    at require (internal/module.js:20:19) 
    at Object.<anonymous> (C:\Users\anand\quFlipApi\app.js:33:19) 
    at Module._compile (module.js:570:32) 
    at Object.Module._extensions..js (module.js:579:10) 
    at Module.load (module.js:487:32) 
    at tryModuleLoad (module.js:446:12) 
    at Function.Module._load (module.js:438:3) 
    at Module.runMain (module.js:604:10) 
    at run (bootstrap_node.js:394:7) 

回答

3

有兩個問題在這裏:

  1. express.Router而不是express.router
  2. 您需要創建一個新的實例express.Router(例如var router = new express.Router()),而不是試圖直接使用構造函數作爲實例。
相關問題