colyseus / uWebSockets-express

Express API compatibility layer for uWebSockets.js
https://npmjs.com/package/uwebsockets-express
MIT License
55 stars 13 forks source link

TypeError: expressify is not a function #29

Open haoynmail opened 1 year ago

haoynmail commented 1 year ago

example: import uWS from "uWebSockets.js" import expressify from "uwebsockets-express"

const uwsApp = uWS.App(); const app = expressify(uwsApp);

// use existing middleware implementations! app.use(express.json()); app.use('/', serveIndex(path.join(dirname, ".."), { icons: true, hidden: true })) app.use('/', express.static(path.join(dirname, "..")));

// register routes app.get("/hello", (req, res) => { res.json({ hello: "world!" }); });

app.listen(8000);

Error: const app = expressify(uwsApp) ^ TypeError: expressify is not a function

Operation process: 1.npm i uWebSockets.js-express 2.npm i uNetworking/uWebSockets.js#v20.31.0

  1. npm i express
  2. npm i -D typescript@5.0.4
endel commented 1 year ago

This works fine - could not reproduce it here.

Please provide a complete example (including package.json and how you run the application). The real issue is likely there.

haoynmail commented 1 year ago

thank you replay code as follow:

server.js: import uWS from 'uWebSockets.js' import expressify from 'uwebsockets-express' import { join } from 'path'

const uwsApp = uWS.App().ws('/', { // handle messages from client open: (socket) => { / For now we only have one canvas */ //socket.subscribe("drawing/canvas1"); },

message:  (socket, message, isBinary) => {
    /* In this simplified example we only have drawing commands */
    socket.send(message, true)
}

})

const app = expressify(uwsApp) app.use(express.static(join(__dirname))) // finally listen using the app on port 9001 app.listen(9001, () => console.log('server listen at localhost:9001'))

package.json:

{ "name": "nodejs-console", "version": "0.0.0", "description": "NodejsConsole", "main": "server.js", "type": "module", "author": { "name": "" }, "dependencies": { "express": "^4.18.2", "uwebsockets-express": "^1.3.4", "uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.31.0" } }

Do: node server

NovaSagittarii commented 1 year ago

Using npm's uWebSockets-express 1.3.4, node v18.9.0

While trying to set up uWS with express, I ran into this issue as well. To fix this, I tried checking what the default export was doing using console.log debugging.

import expressify from 'uwebsockets-express';
console.log(expressify);
/* Prints
{
  Application: [Getter],
  IncomingMessage: [Getter],
  ServerResponse: [Getter],
  Socket: [Getter],
  default: [Function: default_1]
}
*/

Looking into the index.js files, this expressify.default did seem to be the function that accepted a uWS.App and returned an express Application, so I aliased expressify as the default export's default member.

import uWSExpress from "uwebsockets-express"
const expressify = uWSExpress.default;
console.log(expressify);
/* Prints
[Function: default_1]
*/

This seems to be the real expressify function, and I'm able to get the example code working now.

import { fileURLToPath } from 'url';
import path, { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename); // dealing with "type":"module"

import express from 'express';
import uWS from "uWebSockets.js"
import expressify from "uwebsockets-express"

const uwsApp = uWS.App();
const app = expressify.default(uwsApp); // or alias it, either works (.default was added)

// use existing middleware implementations!
app.use(express.json());
// app.use('/', serveIndex(path.join(__dirname, ".."), { icons: true, hidden: true })) // not sure which serveIndex this is from
app.use('/', express.static(path.join(__dirname, "..")));

// register routes
app.get("/hello", (req, res) => {
res.json({ hello: "world!" });
});

app.listen(8000);

You can test this by going to localhost:8000/hello after running the server.

I'm confused why this works, and according to this project's src/index.ts, the intended way should work with the export default function line. https://github.com/colyseus/uWebSockets-express/blob/master/src/index.ts#L4

haoynmail commented 1 year ago

Using the example you provided, ok

thank you

oyed commented 10 months ago

+1 on this, having to do the default manual call - guessing it's due to my server being "type": "module",