Package Exports
- express-ws
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (express-ws) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
express-ws
WebSocket endpoints for express applications. Gives WebSocket connections access to functionality from express middlewares.
Installation
npm install express-ws
Usage
Add this line to your express application:
var expressWs = require('express-ws')(app); //app = express appNow you will be able to add WebSocket routes (almost) the same way you add other routes. The following snippet sets up a simple echo server at /echo.
app.ws('/echo', function(ws, req) {
ws.on('message', function(msg) {
ws.send(msg);
});
});Example
var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);
app.use(function (req, res, next) {
console.log('middleware');
req.testing = 'testing';
return next();
});
app.get('/', function(req, res, next){
console.log('get route', req.testing);
res.end();
});
app.ws('/', function(ws, req) {
ws.on('message', function(msg) {
console.log(msg);
});
console.log('socket', req.testing);
});
app.listen(3000);