CNode

express中怎样实现路由分离,看nodejs开发指南里遇到的一个问题

Mmaksim发布于14 年前最后回复9 年前12 回复20914 浏览0 收藏

报错express没有router()方法

查看回复

回复 (11)

L
leizongmin#1·14 年前

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

U
uandur#3·14 年前

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

C
chrisbi#4·14 年前

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);
A
airyland#5·14 年前

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

R
roshanwu#6·14 年前

app.js 中

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

routes 中

module.exports = function (app) {
  app.get('/', function (req, res) {
      res.render('index', {
	    title: '首页',
	    user: req.session.user
      });
  });
}
I
imzshh#7·14 年前

@RoshanWu 的方法 +1

A
a272121742#8·14 年前

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

K
kshift#9·14 年前
//继续使用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开发指南中的例子出错,目前没有头绪 这里的答案,测试通过。

D
dinghao1994#11·9 年前

娃哈哈,五年前的 坟贴

参与回复
登录后即可参与回复。登录