express - 使用 Express.io 进行路由转发

标签 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

最佳答案

我不能代表 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);

关于express - 使用 Express.io 进行路由转发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15493830/

相关文章:

javascript - 在express.js中设置cookie出现j : prefix

node.js - 使用JWT token 时应如何处理RESTful身份验证?

node.js - Socket.io、Node、Cordova,我在客户端接收器中没有收到消息

node.js - 我如何使用node.js获取用户信息

node.js - Socket.io 从 nodejs Controller 发出,我做得对吗?

node.js - ExpressJS 与 MeteorJS

javascript - 如何使用 express.js 在 Node.js 中获取原始 HTTP header 字符串

php - 用自适应支付取代 paypal express

node.js - 如何在 React 中显示组件并在每次显示时更新其生命周期?

node.js - 不同服务器上的 Socket.IO 房间