app.use('*', function (req, res, next) {
res.locals.date = Date.now();
res.locals.appid = req.query.appid;
res.locals.authtoken = req.query.authtoken;
console.log('appget', res.locals);
exec.emit('appget', res,next);
});
exec.on('appget', async function (res, next) {
console.log('appget');
const key =
'appid:' + res.locals.appid + '|authtoken:' + res.locals.authtoken;
const dbx = locals.svc.db.data.db;
let x = await dbx.get(key);
if (!x) return res.status(401).end();
console.log('result', x);
next();
});
app.get('/login', function (req, res, next) {
res.locals.sessionid = req.query.sessionid;
res.locals.passcode = req.query.passcode;
console.log('login', res.locals);
exec.emit('login', res,next);
});
exec.on('login', async function (res) {
console.log('login');
const key = 'code:' + res.locals.passcode;
const dbx = locals.svc.db.data.db;
let x = await dbx.get(key);
if (!x) return res.status(401).end();
console.log('result', x);
res.status(200).end();
});
My goal is to execute the first route app.use and then the second route final app.get after the next
I get the error : next is not a function
I have been using this in the past, but I don’t understand why it doesn’t work now
Thanks
I was expecting the “next” is a function permitting to go and execute the next route
var Executor = function() {
events.EventEmitter.call(this);
};
util.inherits(Executor, events.EventEmitter);
var exec = new Executor();
exec.setMaxListeners(25);
I fixed the problem
next is added at the end of the exec.emit line
4