当前位置: 首页 > 知识库问答 >
问题:

为什么在express中使用不同的文件进行路由不起作用?

米夕
2023-03-14

我正在尝试模块化我的应用程序分离路由,但唯一有效的是根路由,我不知道我的代码有什么问题,在阅读快速路由后,我的代码似乎写得正确

app.js

// ...
var routes = require('./routes/index');
var users = require('./routes/users');
//Require the external route
var about = require('./routes/about');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

//Routes
app.use('/', routes);
app.use('/users', users);
//The external route 
app.use('login', login);

index.js

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Clazapp' });
});

module.exports = router;

登录名。js公司

var express = require('express');
var router = express.Router();

  //login page
  router.get("/login", function(req, res, next) {
    res.render('login');
  });

module.exports = router;

如您所见,login.js和index.js的代码几乎相同,但是 /login路由不起作用。出现消息

错误:未能查找视图错误在视图目录/home/cesar/Documentos/node/clazapp2/视图node_modules(/home/cesar/Documentos/node/clazapp2/node_modules/Express/lib/_params: 17)在ServerRnode_modules(/home/cesar/Documentos/node/clazapp2/node_modules/Express/lib/node_modules: 7)在 /home/cesar/Documentos/node/clazapp2/app.js:61: 7在Layer.handle_error(/home/cesar/Documentos/node/clazapp2/node_modules/Express/lib/router/layer.js:71: 5)在trim_prefix(/home/cesar/Documentos/node/clazapp2/mitter.render/Express/lib/router/index.js:310: 13)在 /home/cesar/Documentos/node/clazapp2/node_modules/express/lib/router/index.js:280: 7在Function.processapplication.js:579(/home/cesar/Documentos/node/clazapp2/esponse.render/Express/lib/router/index.js:330: 12)在IncomingM(essage.next/home/cesar/Documentos/node/完成(/home/cesar/Documentos/node/clazapp2/node_modules/Express/lib/response.js:956: 25)在EventEmitter.render(/home/cesar/Documentos/node/clazapp2/node_modules/Express/lib/application.js:581: 14)

似乎是文件登录。未找到jade,但他已位于目录/视图中。这么复杂,有什么想法吗?

共有1个答案

公良照
2023-03-14

正如我所知道的快递。路由器()必须仅用于特殊情况。
我使用下一种方式来模块化应用程序

// app.js
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
...
require('./routes')(app); // it's call ./routes/index.js and pass app to it

// ./routes/index.js
var smthEntity = require('./smthEntity');

module.exports = function (app) {
    app.get('/smth-path', ...smth, e.g. checkAuth, ...);
    app.get('/smthEntity', smthEntity.get);

    app.get('/login', require('./login').get);
    app.post('/login', require('./login').post));
}

// ./routes/smthEntity
var SmthEntity = require ('smthEntity'); // include models

exports.get = function(req, res, next) {
    res.render('smthEntity.html', {
        arg1: value1,
        arg2: value2
    }); 
};
... // other method and route for smthEntity

// ./routes/login
exports.get = function(req, res, next) {
    res.render('login.html', { ... });  
};

exports.post = function(req, res, next) {
    ...process post data...
};
 类似资料:
  • 问题内容: 我已经按照教程在我的应用程序中实现了路由 http://docs.angularjs.org/tutorial/step_07 我无法在IE7中使用我的版本,花了一段时间尝试找出我错过/做错的事情后,我注意到该示例不起作用。 http://angular.github.com/angular- phonecat/step-7/app/ 有人知道如何使它工作吗? 问题答案: 好的,我遇到

  • 我正在使用React路由器开发React web应用程序。在我的应用程序中。我已经导入了js文件头和主组件。在home组件中,我有两个组件,分别称为在线银行和信用卡,它们是我从在线银行导入的。js和信用卡。js文件。 当用户单击主页中的链接按钮时。js组件、在线银行和信用卡组件应呈现。 相反,我得到了一个名为error:Invariant failed的错误:您不应该使用 为什么它不起作用? 索引

  • 由于以下错误(npm 3.10.10,webpack 3.8.1),我无法使用vue routes get。如何解决这个问题 编译失败。 ./node_modules/babel loader/lib/node_modules/vue loader/lib/selector.js?type=script 我使用简单的网页包 Quotes.vue Quote.vue new-quote.vue Ap

  • 在尝试让Liberty容器工作时,我遇到了以下问题。 对于数据库连接,我在server.xml中有一个像这样的AuthData部分: 当我试图用未编码的密码运行服务器时,数据库连接按预期工作,但是当密码被编码时,我得到这条消息:连接被拒绝(连接被拒绝)。错误代码=-4499,SQL State = 08001 dsra 0010 e:SQL State = 08001,错误代码=-4499 在设置

  • 问题内容: 我正在尝试使用单独的路线,但在我的React App中添加/编辑表单的组件相同,如下所示: 现在,在manageClient组件中,我解析查询参数(我在编辑路由中传递带有客户端ID的查询字符串),并根据传递的查询参数有条件地进行渲染。 问题在于这不会再次重新安装整个组件。假设打开了一个编辑页面,并且用户单击添加组件,URL发生了更改,但是该组件没有重新加载,因此保留在编辑页面上。 有办

  • 我有一个简单的JavaFX项目。它包括: src/app/main.java src/app/controller/maincontroller.java src/app/controller/secondarywindowcontroller.java src/app/view/main.fxml src/app/view/secondarywindow.fxml 控制器MainControll