为什么使用不同文件进行快速路由并不起作用?

时间:2016-08-01 03:20:59

标签: node.js express

我试图模块化我的应用程序分隔路线,但唯一有效的是根路线,我不知道我的代码的问题是什么,在阅读{{ 3}},我的代码似乎写得正确

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;

login.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路由不起作用。出现消息:

  

错误:无法查找视图"错误"在views目录中   " /家庭/塞萨尔/文件/节点/ clazapp2 /视图"在EventEmitter.render   (/home/cesar/Documentos/node/clazapp2/node_modules/express/lib/application.js:579:17)   在ServerResponse.render   (/home/cesar/Documentos/node/clazapp2/node_modules/express/lib/response.js:961:7)   在/home/cesar/Documentos/node/clazapp2/app.js:61:7 at   Layer.handle_error   (/home/cesar/Documentos/node/clazapp2/node_modules/express/lib/router/layer.js:71:5)   在trim_prefix   (/home/cesar/Documentos/node/clazapp2/node_modules/express/lib/router/index.js:310:13)   在   /home/cesar/Documentos/node/clazapp2/node_modules/express/lib/router/index.js:280:7   在Function.process_params   (/home/cesar/Documentos/node/clazapp2/node_modules/express/lib/router/index.js:330:12)   在IncomingMessage.next   (/home/cesar/Documentos/node/clazapp2/node_modules/express/lib/router/index.js:271:10)   完成了   (/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)

似乎找不到文件 login.jade ,但他已经位于目录/ views中。这么复杂,不管怎样?

1 个答案:

答案 0 :(得分:1)

据我所知,express.Router()必须仅用于特殊情况 我使用下一种方式来模块化应用程序

// 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...
};