Serve Websocket And Http Server At Same Address On Node.js
I have set up a Websocket server on port 8888, using Node.js. I also have an interface for that which interacts with the Websocket backend (chat server). How do I serve the index.
Solution 1:
If you don't want to use socket.io
, but the websocket
package, you can use it in combination with Express like this:
// app.jsvar WebSocketServer = require('websocket').server;
var express = require('express');
var app = express();
var server = app.listen(8888);
var wsServer = new WebSocketServer({ httpServer : server });
// this will make Express serve your static files
app.use(express.static(__dirname + '/public'));
// the rest of your code
wsServer.on('request', function(r) { ...
express.static
will take care of serving your HTML/CSS/JS files. The argument you pass is the directory where those files are located (in this case, the directory public/
relative to where app.js
is located).
Solution 2:
Here are some good gist's how the set up a websocket server incl. serving an webapp for that:
- https://gist.github.com/johnhunter/1456613 (express.js + socket.io)
- https://gist.github.com/damianb/4190316 (expressjs + socket.io + passport + redis)
Here are also similar questions:
Post a Comment for "Serve Websocket And Http Server At Same Address On Node.js"