In index.js I have:
var express = require('express');
var router = express.Router();
const { codeExecute } = require('../controllers/userController');
router.get('/', function(req, res, next) {
if (req.query.code){ // if there is "code" param in the get request
console.log(req.query.code) // here it correctly prints if there is "code" in the request
codeExecute(req,res,next) // here I'd like to pass the flow control to this function
}else{
res.render('index', { title: 'La mia Home Page' });
}
});
In userController.js I have:
async function codeExecute(req, res, next) {
try {
console.log(req.body.code) // this prints undefined
//do stuff, for example res.render something
} catch (err) {
console.log(err);
next(createError(500));
}
}
Can I pass the flow control to another function that will render inside a router?