Petar Ivcec
Petar Ivcec

Reputation: 756

pass argument to Express route/module

I have an Express server that creates a WSS server like so:

const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: '8080' });

wss.on('connection', async (ws, req) => { 
 ...
})

// routes setup
const chatRouter = require('./routes/chatPoint')(wss);

app.use('/api/chat', chatRouter);

I am attempting to pass argument 'wss" to the following module:

router.patch('/title', jsonParser, async (req, res) => {
   ...
});

module.exports = router;

The current error that I am getting is:

    return fn.apply(this, arguments);
              ^
TypeError: Cannot read property 'apply' of undefined

Please advise.

Upvotes: 0

Views: 47

Answers (1)

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64943

Use app.locals. Set a property and you'll be able to access it from the request object as req.app.locals from any middleware.

Upvotes: 2

Related Questions