我在Socket.io中使用Express,但我不知道如何在Express路由中使用SocKet.io。
我最终在“ app.js”中这样做
...
...
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
var server = http.createServer(app);
var io = require("socket.io").listen(server);
app.get('/', routes.index);
app.get('/users', user.list);
app.post('/cmp', function(request, response) {
var client = new pg.Client("pg://user:pass@127.0.0.1/db_name");
client.connect(function(err) {
// Get the product_id and bid
var product_id = request.body.product_id;
var bid = request.body.bid.split('b')[1];
// If not get the connection
if(err) {
return console.error('could not connect to postgres', err);
}
client.query('select 1 from product_bid where product_id = $1 and number_bid = $2', [product_id, bid], function(err, result) {
if(err) {
return console.error('error running query', err);
}
if (result.rowCount == 1) {
// do not insert
} else {
// insert
// Insert to the DB
client.query('insert into product_bid (product_id, number_bid) values ($1, $2)', [product_id, bid], function(err, result) {
if(err) {
return console.error('error running query', err);
}
io.sockets.emit("bidSuccess", {product_id: product_id, bid: bid});
response.json(200, {message: "Message received!"});
client.end();
});
}
});
});
});
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
// ---------------
io.on('connection', function(socket){
console.log("alguem se ligou!");
socket.emit('event_from_server', {message: 'conectou-se ao servidor'});
});
我该如何定义到“ / cmp”的路由,并在其中传递var“ io”?
app.post('/cmp', routes.cmp);
这样,在“ /routes/cmp.js”中,我可以执行以下操作:
exports.cmp = function(req, res){
var product_id = req.body.product_id;
var bid = req.body.bid.split('b')[1];
io.sockets.emit("bidSuccess", {product_id: product_id, bid: bid});
response.json(200, {message: "Message received!"});
};
一些线索?
高阶函数呢?
exports.cmp = function(io) {
return function(req, res){
var product_id = req.body.product_id;
var bid = req.body.bid.split('b')[1];
io.sockets.emit("bidSuccess", {product_id: product_id, bid: bid});
response.json(200, {message: "Message received!"});
}
};
然后
app.post('/cmp', routes.cmp(io));
作为另一种选择,有时我会将路线的格式设置为以下格式:
var routes = require('./routes/routes');
routes(app, io);
然后定义routes
为
module.exports = function(app, io) {
app.post('/cmp', function(req, res){
var product_id = req.body.product_id;
var bid = req.body.bid.split('b')[1];
io.sockets.emit("bidSuccess", {product_id: product_id, bid: bid});
response.json(200, {message: "Message received!"});
})
};
问题内容: 我正在尝试将Socket.io与Node.js结合使用,并在路由逻辑内发送到套接字。 我有一个相当标准的Express 3安装程序,其中的server.js文件位于该路由中,然后我的index.js位于一个routes文件夹中,该文件夹导出站点的所有页面/公共可访问功能。因此,它们看起来像: 在server.js中定义的路由如下: 我假设我必须在server.js中创建socket.i
我正在考虑将Socket.io集成到一个express应用程序中。 js有一个非常好的特性,可以通过socket.io消息调用快速路由。 不过,帆在其他方面比我需要的要多一点。我正在寻找一种方法,使socket.io请求转发到快速路由,而不必使用整个sails框架。我想这是一个很常见的需求,所以我很惊讶我没有找到一个npm模块来做这件事,但是找了很长时间,我什么也没有找到。Express.io会这
问题内容: 我用nodejs,express和htt-proxy编写了一个小型代理。它适用于提供本地文件,但在代理外部api时失败: 问题是yahoo api没有响应,也许有响应,但我没有出现在浏览器中。 问题答案: 和-Package 甚至更简单 它将整个请求传递给API,并将响应传递回请求者。这也处理POST / PUT / DELETE和所有其他请求\ o / 如果您还关心查询字符串,则也应
问题内容: 我有一个像防火墙/调度程序一样位于其他微服务前面的节点应用程序,它使用如下所示的中间件链: 但是对于特定的GET路由,我想跳过所有这些,除了rateLimiter和proxy。他们是否可以使用:except /:only来设置类似Rails before_filter的过滤器? 问题答案: 即使expressjs中没有内置的中间件过滤器系统,您也可以通过至少两种方法来实现。 第一种方法
我们将结束数据结构和算法的部分,并将数据结构用于实际问题。我已经写了几个 Web 服务器,一个不断出现的问题是,将 URL 路径匹配到“动作”。你会在每个 Web 框架,Web 服务器,和必须基于层次化的键来“路由”信息的任何东西中发现此问题。当你的 Web 服务器收到URL /do/this/stuff/时,必须确定每个部分是否可能附加了某种操作或配置。如果你在/do/配置了 Web 应用程序,
问题内容: 这是我的代码,但显示了进度。这段代码有什么错误吗?请提供一些想法来解决此问题,或者提供一些与此相关的链接。 问题答案: 更新的答案: 要关闭ProgressHUD: