简体   繁体   English

尝试将获取请求从控制器模块导入到主应用程序时出现错误。 (类型错误:无法读取未定义的正确“获取”)

[英]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.

相关问题 提交空搜索请求时收到错误“TypeError:无法读取未定义的属性“地图” - I get the error 'TypeError: Cannot read property 'map' of undefined' when submitting a empty search request 我收到错误:运行我的反应项目时无法读取未定义的属性'then' - I get the error : Cannot read property 'then' of undefined when I run my react project 当我尝试使用tensorflow.js模型进行预测时,出现错误:Uncaught TypeError:无法读取未定义的属性“ length” - When I try to make a prediction using a tensorflow.js model I get the error: Uncaught TypeError: Cannot read property 'length' of undefined 运行我的nodejs代码时收到此错误:'TypeError:无法读取未定义的属性'apply'。 ' - I get this error when running my nodejs code: ' TypeError: Cannot read property 'apply' of undefined. ' 当我尝试将 HTML 文件导入 React 时,出现错误 - When I try to import an HTML file into React, I get an error 当我想在本地服务器上运行我的代码时,我总是会收到这种错误“SyntaxError: Cannot use import statement outside a module” - When I wants to run my code in local server i always get this kinds of error “SyntaxError: Cannot use import statement outside a module” 我的 Angular 11 cli 应用程序无法与 jest 一起使用。 我得到的错误:TypeError:无法读取未定义的属性“路径” - My Angular 11 cli app not working with jest. Error I get: TypeError: Cannot read property 'paths' of undefined 我在Node.JS应用程序中使用路由,当我尝试从浏览器中打开其中一个路由时,出现错误 - I use routes in my Node.JS app and when I try to open one of the routes from the browser I get an error 当我尝试 type="module" 时出现此错误(未捕获的 ReferenceError:loadFoods 未定义) - When I try type="module" get this Error ( Uncaught ReferenceError: loadFoods is not defined) 为什么在尝试发送带有标题的请求时出现错误? - Why i get error when try post request with headers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM