问题描述
我正在使用express.js 4创建一个非常简单的节点API,但是我需要一些实时事件,我添加了socket.io。我很新,所以我可能错过了一些基本的东西,但我找不到很好的文档/ tuts。
在快速应用程序快速生成器)我有一些这样的基于简单的例子和项目文档,我读。这可以正常工作,从客户端应用程序,我可以发送/接收套接字事件:
c code code code code code code code code code code code $ c $ pre> var express = require('express');
var router = express.Router();
var io = ???;
router.put('/ foo',function(req,res){
/ *
做更新foo资源的东西
...
* /
//现在播放更新的foo ..
io.sockets.emit('update',foo); //如何?
});
module.exports = router;
修改你的文件一点点,你可以检查它是否有效?
您可以将您定义的io您的路线传递到下面;
require('./ routes / api')(app,io);
我没有测试Socket.IO部分b没有语法错误,路由也工作。
server.js文件:
var express =表现');
var app = express();
var path = require('path');
var logger = require('morgan');
var io = require('socket.io')。listen(app.listen(3000));
app.use(logger('dev'));
app.use(express.static(path.join(__ dirname,'public')));
io.sockets.on('connection',function(socket){
console.log('client connect');
socket.on('echo',function (数据){
io.sockets.emit('message',data);
});
});
require('./ routes / api')(app,io);
console.log(端口3000服务器侦听);
api.js:
module.exports = function(app,io){
app.put('/ foo',function(req,res){
/ *
做更新foo资源的东西
...
* /
//现在广播更新的foo ..
console.log(PUT OK!);
io.sockets.emit('update'); //如何?
res.json({result:通过IO发送更新});
});
}
I'm trying to create a really simple node API using express.js 4 but I need a few 'realtime' events for which I added socket.io. I'm fairly new to both so I'm likely missing something basic but I can't find good docs/tuts on this.
In the express app (created with the express generator) I have something like this based on simple examples and project docs that I read. This works OK and from client apps, I can send/receive the socket events:
var express = require('express'); var path = require('path'); var logger = require('morgan'); var api = require('./routes/api'); var app = express(); var io = require('socket.io').listen(app.listen(3000)); app.use(logger('dev')); app.use(express.static(path.join(__dirname, 'public'))); app.use('/api', api); io.sockets.on('connection', function (socket) { console.log('client connect'); socket.on('echo', function (data) { io.sockets.emit('message', data); }); }); // error handlers omitted module.exports = app;
but I want to use the sockets from my API routes (in the ./routes/api.js file that I 'require' above). For example, someone might use the API to PUT/POST a resource and I want that broadcast to connected socket.io clients.
I cannot see how to use the 'io' variable or organise the code currently in the io.sockets.on('connection' ... function inside express routes. Here's the ./routes/api.js file:
var express = require('express'); var router = express.Router(); var io = ???; router.put('/foo', function(req, res) { /* do stuff to update the foo resource ... */ // now broadcast the updated foo.. io.sockets.emit('update', foo); // how? }); module.exports = router;
I've modified your files a little bit, may you check if it works?
You can pass the io you've defined to your routes like below;
require('./routes/api')(app,io);
I didn't test the Socket.IO parts but there is no syntax error and routes also working.
server.js file:
var express = require('express'); var app = express(); var path = require('path'); var logger = require('morgan'); var io = require('socket.io').listen(app.listen(3000)); app.use(logger('dev')); app.use(express.static(path.join(__dirname, 'public'))); io.sockets.on('connection', function (socket) { console.log('client connect'); socket.on('echo', function (data) { io.sockets.emit('message', data); }); }); require('./routes/api')(app,io); console.log("Server listening at port 3000");
api.js:
module.exports = function(app,io) { app.put('/foo', function(req, res) { /* do stuff to update the foo resource ... */ // now broadcast the updated foo.. console.log("PUT OK!"); io.sockets.emit('update'); // how? res.json({result: "update sent over IO"}); }); }
这篇关于express.js 4和带快速路由器的套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!