colyseus / colyseus-social

Authentication and Social features
https://docs.colyseus.io/tools/colyseus-social/
MIT License
21 stars 17 forks source link

Router.use gets object instead of function when requring socialRoutes #21

Closed merhawie closed 4 years ago

merhawie commented 4 years ago

Hi -- I seem to be getting an error in importing (rather requiring) because I am node 9.7.1

Here is the error trace that I am getting: /home/runner/GamesServer/node_modules/express/lib/router/index.js:458 throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) ^

TypeError: Router.use() requires a middleware function but got a Object at Function.use (/home/runner/GamesServer/node_modules/express/lib/router/index.js:458:13) at Function. (/home/runner/GamesServer/node_modules/express/lib/application.js:220:21) at Array.forEach () at Function.use (/home/runner/GamesServer/node_modules/express/lib/application.js:217:7) at Object. (/home/runner/GamesServer/index.js:58:5) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

seiyria commented 4 years ago

Can you provide the code you're using? Without that it will be hard to diagnose what's wrong.

merhawie commented 4 years ago

Wow -- amazing dev comm. I have extracted what I think are the relevant parts of my app.


index.js


require('dotenv').config(); ... const mongoose = require('mongoose'); const express = require("express"); const colyseus = require("colyseus"); const socialRoutes = require("@colyseus/social/express"); const { GameRoom} = require('./GameRoom'); ...

(async () => { try { await mongoose.connect(process.env.MONGO_URI, { user: process.env.MONGO_USER, pass: process.env.MONGO_PASSWORD, useNewUrlParser: true, useFindAndModify: false, useCreateIndex: true, useUnifiedTopology: true }); } catch (err) { console.log('error: ' + err) } })();

const app = express(); app.use("/", socialRoutes);

const gameServer = new colyseus.Server({ server: http.createServer(app), express: app }); ...


GameRoom.js


const colyseus = require('colyseus'); const { User, verifyToken } = require("@colyseus/social");

const { State } = require('./schemas/State');

const Gameroom = class extends colyseus.Room { onCreate (options) { this.setState(new State()); console.log("This room ("+this.roomId+") was created"); } async onAuth (client, options, request) { const token = verifyToken(options.token);

    return await User.findById(token._id);
}

...

endel commented 4 years ago

Hi @merhawie, when using require, you need to import the default value:

const socialRoutes = require("@colyseus/social/express").default

The npm init colyseus-app template has this: https://github.com/colyseus/create-colyseus-app/blob/javascript/index.js#L6

We need to add this to the docs (https://docs.colyseus.io/tools/colyseus-social/), you're not the first one to have this issue.