Calling Express Route internally from inside NodeJS

The ‘usual’ or ‘correct’ way to handle this would be to have the function you want to call broken out by itself, detached from any route definitions. Perhaps in its own module, but not necessarily. Then just call it wherever you need it. Like so:

function updateSomething(thing) {

// elsewhere:
router.put('/api/update/something/:withParam', function(req, res) {
    .then(function() { res.send(200, 'ok'); });

// another place:
function someOtherFunction() {
    // other code...
    // ..

Leave a Comment