express中怎样实现路由分离,看nodejs开发指南里遇到的一个问题
发布于 12 年前 作者 maksim 19511 次浏览 最后一次编辑是 8 年前

报错express没有router()方法

11 回复

99%可能是express版本不一致问题,新版本使用的是app.use(app.router); 一般你现在装的Express为3.x版本,而大多数的教程所说的express为2.x版本。 安装2.x版本: npm install express@2.5.11 或者到这里看新版本的教程:http://expressjs.com/

你能给一个分离的例子吗?

不分离呗,那是原来那样写。

app.js还是原来这样写啊

app.get('/', routes.index);
app.get('/index',routes.index);
app.get('/u/:user',routes.user);
app.post('/post',routes.post);
app.get('/reg', routes.reg);
app.post('/reg',routes.doReg);
app.get('/login',routes.login);
app.post('/login',routes.doLogin);
app.get('/logout',routes.logout);

人类已经无法阻止cnodejs被XSS了!

app.js 中

require('./routes')(app);

routes 中

module.exports = function (app) {
  app.get('/', function (req, res) {
      res.render('index', {
	    title: '首页',
	    user: req.session.user
      });
  });
}

广告贴,使用老吴@snoopy的rrestjs框架吧,真的很好用哦~

//继续使用app.use(app.router),在app.js最后执行一下routes(app);即可

app.configure('development', function(){
  app.use(express.errorHandler());
});

routes(app);//这里执行

// routes/index.js 还按书上的来
module.exports=function(app){

  app.get('/',function(req,res){
    res.render('index',{
      title:'首页'
    });
  });

};

引用 运行Node.js开发指南中的例子出错,目前没有头绪 这里的答案,测试通过。

同意,我的也利用这个方法运行成功

娃哈哈,五年前的 坟贴

回到顶部