使用Express.io进行路由转发

时间:2013-03-19 07:34:46

标签: express socket.io

我尝试使用Express.io转发路由,但它不起作用,我在Github上阅读了文档,我按照他们的说法完成了。我不知道问题出在哪里......

app.post('/signin', function(req, res) {
    me.pseudo = req.body.pseudo;
    me.email = req.body.email;
    me.gravatar = "http://www.gravatar.com/avatar/" + md5(me.email) + "?s=140";
    users.push(me);
    req.io.route('hello'); //error here
});

app.io.route('hello', function(req) {
   console.log('Done !'); 
});

错误:

TypeError: Cannot call method 'route' of undefined
    at /Users/anthonycluse/Sites/Tchat-Express/app.js:78:12

1 个答案:

答案 0 :(得分:0)

我不能代表app.io,但通常当你需要在路线之间分享相同的功能时,你要么 a)使错误处理程序成为一个单独的函数,并从多个路径调用它:

function handleError(req, res) {
  //handle error
}

app.post('/foo', function(req, res) {
  //if conditions are met, else
  handleError(req, res); 
});

b)通过制作模块进一步摘要:

//user.js
module.exports = {
  signin: function(req, res) {},
  signinError: function(req, res) {},
};

路由代码

//routes.js
var user = require('../modules/user');

app.post('/signin', function(req, res) {
  //validate signin
  //else
  user.signinError(req, res);
});

app.post('/signin-no-error', user.signin);
相关问题