我如何修复路由以查找node.js中子文件夹内的视图

时间:2013-07-02 12:02:58

标签: node.js express routes views subdirectory

我正在尝试为localhost创建路由:port / admin / 我想将routes.js文件和view.js文件保存在匹配的文件夹中,以便我以后不会有太多的spaggeti

但我一直得到:500错误:无法查找视图“/ admin / manage_subjects” 尝试创建新路由并使用相同的文件夹

我有以下带快递的视图文件夹

mainapp(root)
  routes(folder)
    admin(folder)
      index.js(nested inside admin)
    index.js(nested inside routes)
  views(folder)
    admin(folder)
      admin_layout.jade(nested inside admin)
      manage_subjects.jade(nested inside admin)
    index.jade(nested inside views)
    layout.jade(nested inside views)

代码: 路由/管理/ index.js

exports.index = function (req, res) {
res.render('manage_subjects',{title:'Express'});}

视图/管理/ manage_subjects.jade

extends admin_layout
block content
h1 = title
p Welcome to #{title}

我的app.js代码

/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes')
  , admin_routes = require('./routes/admin/')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path')
  , repository = new (require('./domain_model/repository'))();

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

//fill local subjects
repository.subjects.GetAll(function (err, data) {
    if (err) throw err;
    app.locals.subjects = data;
});

//append routes
app.get('/', routes.index);
app.get('/admin', admin_routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on http://localhost:' + app.get('port'));
});

3 个答案:

答案 0 :(得分:14)

我一直在处理我认为同样的问题,并想出如何解决它。因此,如果有其他人遇到这个问题我会发布我的解决方案。

所以这就是我所拥有的导致404和500的

app.js

var routes = require('./routes/index');
var admin = require('./routes/admin');

app.use('/', routes);
app.use('/admin', admin);

这是我的routes / index.js

//append routes
var express = require('express');
var router = express.Router();

router.get('/', function(req, res) {
    res.render('index', {title: 'Express'});
});

module.exports = router;

和我的路线/ admin.js:

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

router.get('/admin', function(req, res) {
    res.render('admin/index', {title: 'Express'});
});

module.exports = router;

通过在router.get()函数中定义第二个/ admin我认为我有效地告诉节点在我的views文件夹中查找以下路径视图/ admin / admin / index.ejs中的html。所以要解决这个问题,我要做的就是从router.get()中删除/ admin,或者从app.use()中删除/ admin 所以我的工作代码现在看起来像这样:

app.js

var routes = require('./routes/index');
var admin = require('./routes/admin');

app.use('/', routes);
app.use('/admin', admin); //I left the /admin here and instead removed the one in routes/admin.js

这是我的routes / index.js

//append routes
var express = require('express');
var router = express.Router();

router.get('/', function(req, res) {
    res.render('index', {title: 'Express'});
});

module.exports = router;

和我的路线/ admin.js:

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

router.get('/', function(req, res) { //NOTICE THE CHANGE HERE
    res.render('admin/index', {title: 'Express'});
});

module.exports = router;

因此,进行更改后,我的视图文件夹中可以包含子文件夹。

答案 1 :(得分:1)

我会查看TJ关于模块化应用on his vimeo的视频,关于此工作流程的最佳部分是您的代码变得非常灵活,并且更容易留下DRY

此外,我会使用app.set("views")

执行此类操作
var path = require("path");
app.set('views', path.join(__dirname, 'views'));

// you can then extend this to the example for routes

另一个替代方案就像在app.js文件中一样:

var express require("express")
var app = express()
var routes = require("./path/to/routes")(app)

然后路线看起来像:

routes = function (app) {
  app.get("/route", middleWareifYou.gotIt, route.handler || function (req, res) {
    res.send("some msg");
  });
};

module.exports = routes

干杯,希望这有帮助!

答案 2 :(得分:0)

简单答案,用于views文件夹中的子文件夹(我的称为前端)

click here the picture to see the folder structure

$ readelf --string-dump=.gnu_debuglink helloworld | sed -n '/]/{s/.* //;p;q}'
helloworld