Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
How to segment model from controller in a node api?

I'm trying to keep my node/express api clean, but am unable to keep code effectively separate.

Most tutorials cram the data operation + response into index.js like so:

app.post('/user', (req res) => {
   db.query("SELECT UserID, UserName FROM User", function (err, result, fields) {
      if (err) throw err;
      res.json(response);
   });
});

This is fine for simple tutorials, but once things get complex, I get a huge index.js file.

I'd prefer to separate User into its own class and call on it to perform data operations. However querying the database requires a promise - and I don't know how to get the data back out to the original calling function. This leaves me passing the response function in, forcing the User class to behave as both model and a little controller in MVC.

app.post('/user', (req res) => {
   var user = new User(db);
   user.getUserList(res);
   // I wish I could get the userlist back and respond here...
   });
});

(User.js)

getUserList(res) {
   db.query("SELECT UserID, UserName FROM User", function (err, result, fields) {
      if (err) throw err;
      //...but since I don't know how to get data back out of this method, I'll respond here.
      res.json(response);
   });
}

Is this just how it is in node.js? Is there a design pattern to keep these separate in node?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I see two problems here, one that your index.js file keeps growing; you should be splitting your code. There's a great tutorial on this here. secondly, you should use async/await to return results from DB. Here's how you can achieve it.

User.js

async function getUserList() {
    return db.query("SELECT UserID, UserName FROM User", function (err, result, fields) {
        if (err) throw err;
        return result;
    });
}

Controller function

app.post('/user', async (req res) => {
    var user = new User(db);
    const userList = await user.getUserList(res); // this will wait for the function to return the response
    res.json(userList);
});
about 4 years ago · Juan Pablo Isaza Relatório

0

What you could do is split your controllers, models and routes declarations into separate folders.

- /models
 -- user.model.js
- /controllers
 -- user.controller.js
- /routes
 -- user.route.js

Your user.model will contain the model declaration.

The user.controller will contain the functions responsible for handling an end-point operation:

const getUserList = (req, res) => {
   db.query("SELECT UserID, UserName FROM User", function (err, result, fields) {
      if (err) throw err;
      res.json(response);
   });
}

module.exports = { getUserList }

The user.route will contain the code responsible for setting up your route declarations with Express Router:

const express = require('express')
const router = express.Router()

const { getUserList } = require('../controllers/user.controller')

router.get('/', getUserList)

module.exports = router;

Finally, in your app.js, you will just need to setup your routes with:

app.use('/user', require('./routes/user.route'))
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda