express.router 报错 解决办法
发布于 11 年前 作者 yanyumiao 5674 次浏览 最后一次编辑是 8 年前

app.js:

/*************************** Begain of app.js ******************************** /

var express = require(‘express’);

// 路由文件 var routes = require(’./routes’); // 默认路由文件 index.js var my = require(’./routes/my’); // Notice: my.js是自定义的路由文件 var http = require(‘http’); var path = require(‘path’); var util = require(‘util’); var partials = require(‘express-partials’); var app = express();

// all environments app.set(‘port’, process.env.PORT || 3000); app.set(‘views’, path.join(dirname, ‘views’)); // 页面模板位置 app.set(‘view engine’, ‘ejs’); // 模板引擎 app.use(partials()); app.use(express.favicon()); app.use(express.logger(‘dev’)); app.use(express.json()); app.use(express.urlencoded()); app.use(express.methodOverride()); app.use(express.cookieParser(‘your secret here’)); app.use(express.session()); app.use(app.router); // Notice app.use(express.static(path.join(dirname, ‘public’)));

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

// 路由 注意先后顺序 routes(app); my(app);

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

/*************************** End of app.js ****************************** /

my.js: // 路由文件

/************************** Begain of my.js ********************************/

module.exports = function(app) { app.get(’/my’, function(req, res) { res.send(‘This is the mongodb page for test.’); });

} /****************************** End of my.js *************************/

4 回复

不是app.use(app.router)的错,而是启动静态文件的错 应该是app.use(express.static(path.join(__dirname, ‘public’)));

回到顶部