Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with router and example code not working? #152

Open
ata1704 opened this issue Oct 11, 2021 · 3 comments
Open

Problem with router and example code not working? #152

ata1704 opened this issue Oct 11, 2021 · 3 comments

Comments

@ata1704
Copy link

ata1704 commented Oct 11, 2021

Hi. I tried to implement express-ws in my existing project. After it failed it tried the example in a new project. I even done it all in one file and it's not working:

var express = require('express');
var app = express();
var expressWs = require("express-ws")(app);

app.ws('/echo', function(ws, req) {
    ws.on('message', function(msg) {
        ws.send(msg);
    });
});

var router = express.Router();

router.ws('/echo', function(ws, req) {
    ws.on('message', function(msg) {
        ws.send(msg);
    });
});

app.use("/ws-stuff", router);

It's always telling me that "ws" on "router.ws" is an unresolved function.

@ruokid
Copy link

ruokid commented Dec 27, 2021

显然

var expressWs = require("express-ws")(app);

只适用于app这个变量

@pepesoriagarcia99
Copy link

Hello, I have the same problem. There is no way to apply on the Router

@wistoft
Copy link

wistoft commented Jul 15, 2023

This could be caused by express-ws adding the ws function to one version of express in node_modules, and the script getting another version of express from node_modules.

This way app.ws will work, because express.ws gets app as parameter, but router.ws will not work, because it's another version of express.

The solution would be to ensure, there's only one version of express in the codebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants