Controllers and Routes in Node js

Asked

Viewed 1,006 times

2

I wonder if it implies any difference between using only routes, or routes with controllers in the Node js, could someone give me some example of how to implement routes and controllers on Node using express js?

1 answer

3


There’s a difference between using only routes, and using routes with controllers, the difference is if you need logic on the server or if you’re serving static content that doesn’t need logic.

The simplest example is using express to serve static files, no routes, no logic. Simply mapping:

app.use('/', express.static('minha-diretoria-root-do-site'))

Imagine you have two HTML files you want to send to the client. In practice you always have a controller, but in this case he does so little that we will say that it is only a route to serve files.

Example with no logic to justify a controller:

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, '../minha-diretoria-root-do-site', 'index.html'));
});
app.get('/contacto', (req, res) => {
    res.sendFile(path.join(__dirname, '../minha-diretoria-root-do-site', 'contacto.html'));
});

Example where you need a controller, which may be in the same file as the example here, or in different files if it is extended:

app.get('/casas', (req, res) => {
    const query = 'SELECT name, price, rooms FROM houses';
    db.query(query, (err, rows) => {
        Promise.all([...rows].map(converterCambio)).then(items => {
            res.render('items', items);
        });
    });     
});

In this last example you do 2 asynchronous steps, and here the thing can complicate and become extensive, hence the need for controller. In the example I imagine that I send to the renderer, such as the ejs, or Pug to compile the page with the array I sent you.

  • Okay, but this also applies to operations with CRUD ?

  • @William explains better what you have in mind. In order to have CRUD you must have a logical server to handle these requests. Ccreate a logical database entry, Read may eventually have less logic, but it all depends on the complexity of the application, Update needs logic and Dhe needs logic.

  • would be something as simple as a student register, which would require operations with the bank with select, update, Insert and delete, soon to have a better organization I would like to implement controllers.

  • @William controler is nothing more than a logical function like the ones I gave in the example. You can have more or less code and have it in the same file or not. If there’s any part you can’t implement say/question, but I think that’s another question. This is "difference between using only routes, or routes with controllers". I have taste (and certainly others too) in helping/explaining more.

  • Yes @Sergio, I understood that controllers in general they are functions however, in this case my doubt would be how to take the data of a form and for that would use the for example app.get('aluno', function(res, req) in the controller and when called on the route there would be no duplication on the route?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.