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

TypeError: expressify is not a function #29

Open
haoynmail opened this issue Jul 29, 2023 · 5 comments
Open

TypeError: expressify is not a function #29

haoynmail opened this issue Jul 29, 2023 · 5 comments

Comments

@haoynmail
Copy link

haoynmail commented Jul 29, 2023

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
3. npm i express
4. npm i -D [email protected]

@endel
Copy link
Member

endel commented Jul 31, 2023

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
Copy link
Author

haoynmail commented Aug 1, 2023

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
Copy link

NovaSagittarii commented Aug 12, 2023

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
Copy link
Author

Using the example you provided, ok

thank you

@devlsh
Copy link

devlsh commented Nov 2, 2023

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

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