Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
How to call a function (in separate file) from http callback function. app.get("/", callbackFun)

File structure:

testAPI
    contactDetail
        dispMobNo.js
        myModule.js
index.js

index.js

const express = require("express");
const app = express();
const port = process.env.port || 3000;
const getCustNo = require("./contactDetail/dispMobNo");
app.use(getCustNo);

app.listen(port, console.log("server running on port: " + port));

dispMobNo.js

const express = require("express");
const router = express.Router();
router.get("/", getContactNo);

function getContactNo(req, res)
{
    console.log(req.query);
    res.send(req.query);
}
module.exports = router;

Above code is working BUT I want to remove getContactNo function from this file and want to put it in a separate file. I mean, I don’t want to put any code inside/under API endpoint. I want to put getContactNo function in separate file and want to call from API endpointcallback function. So, how to do this?

Modified version of dispMobNo.js:

const express = require("express");
const router = express.Router();
router.get("/", getContactNo);
module.exports = router;

myModule.js

function getContactNo(req, res)
{
    console.log(req.query);
    res.send(req.query);
}
module.exports = getContactNo;
about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

you can do this way

  1. create one controller file in which create and export all your business logic functions.

// controller.js file

export async getData = (req,res) => {

// your logic


}

  1. import that controller file in your route file, and use function there.

// route.js file
const Controller = require("./controller");

route.get('/api-endpoint',async(req,res)=>{
  Controller.getData(req,res);
})

EDIT for simpler explanation

// controller.js file
function getData(req,res){
 //your logic
}
module.exports = {getData}
    
// main route file
const {getData} = require('./controller.js');
 route.get('/api-endpoint',function(req,res){
getData(req,res);
});
about 4 years ago · Santiago Trujillo Denunciar

0

You are almost there.

Modified version of dispMobNo.js:

const myModule = require('./my-module.js');
const express = require("express");
const router = express.Router();
router.get("/", myModule.getContactNo);
module.exports = router;

my-module.js

function getContactNo(req, res)
{
    console.log(req.query);
    res.send(req.query);
}
module.exports = {
  getContactNo
};

Above, I have assumed that, both files are in same directory.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda