[英]I get an error when I try to import a get request from a controller module to my main app. (Type-error : Cannot read proper 'get' of undefined)
This is my index.js
file in my ./home
directory: 这是我的
./home
目录中的index.js
文件:
var express = require('express');
var control = require('./controllers/todoController');
var app = express();
//set up template engine
app.set('view engine', 'ejs');
//static files
app.use(express.static('./public'));
//fire controllers
control();
//listen to port
app.listen(3000);
console.log('You are listening to port 3000');
This is my todoController.js
file in my ./home/controllers
directory: 这是我的
todoController.js
目录中的./home/controllers
文件:
module.exports =function(app){
app.get('/quiz', function(req, res){
res.render('quiz');
});
};
The error that is shown is: 显示的错误是:
TypeError : cannot read property 'get' of undefined
You must give your app
to controller
as parameter 您必须将
app
作为参数提供给controller
Try this in your index.js
file 在
index.js
文件中尝试
//fire controllers
control(app);
Use the router module of expressjs 使用expressjs的路由器模块
const express = require('express'); const router = express.Router(); /* GET home page. */ router.get('/', function(req, res, next) { var token = req.body.token('token'); if(!token) { res.render('error', { 'message': "You must indicate a Token" }); } }); module.exports = router;
Then, just import it on your app.js main file : 然后,将其导入到您的app.js主文件中:
app.use('/', tokenHandler);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.