G
G
gallantalex2017-02-13 17:42:43
Node.js
gallantalex, 2017-02-13 17:42:43

How does routing work in Express?

Help with routing in Node.js Express framework.
I am writing an application - a note manager, and at this time there are routings for all CRUD-operations of notes.

app.get('/notes', (req, res)=> {
    db.listNotes().then(data => res.send(data));
});

app.post('/notes', (req, res)=> {
    db.createNote(req.body).then(data => res.send(data));
});

app.delete('/notes/:id', (req, res)=> {
    db.deleteNote(req.params.id).then(data => res.send(data));
});

app.put('notes/:id', (req, res)=> {
   db.updateNote(req.params.id).then(data => res.send(data));
});

These are 4 note-taking functions. And if I need to add the "Users" entity, and then "Themes", will I need to add 4 more functions for each model? Are there any methods for optimizing such tasks? Or should the main entry point to the application contain such monotonous functions?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
emp1re, 2017-02-13
@emp1re

same functions?

All functions are different for you, move your logic to NotesController.
And do not forget that this is a training example and such a cb will be longer than 1 line.
I advise you to look at what middleware is and how to process errors in express, this immediately hurts the eye

D
Damir Makhmutov, 2017-02-13
@doodoo

After all, you can put it in a separate function, and do something like:

function crud(app, path, controller) {
    app.get(path, controller.get);
    app.post(path, controller.post);
    app.delete(`${path}/:id`, controller.delete);
    app.put(`${path}/:id`, controller.put);    
}

class NotesController {
    get(req, res) {
        db.listNotes().then(data => res.send(data));
    }

    post(req, res) {
        db.createNote(req.body).then(data => res.send(data));
    }

    delete(req, res) {
        db.deleteNote(req.params.id).then(data => res.send(data));
    }

    put(req, res) {
        db.updateNote(req.params.id).then(data => res.send(data));
    }
}

crud(app, '/notes', new NotesController);
crud(app, '/users', new UsersController);

N
nirvimel, 2017-02-13
@nirvimel

Uncompromising adherence to the DRY principle:

[
    ['get', '/notes', 'listNotes'],
    ['post', '/notes', 'createNote'],
    ['delete', '/notes/:id', 'deleteNote'],
    ['put', '/notes/:id', 'updateNote'],
].map(([app_method, route, db_method]) => {
    app[app_method](route, (req, res) => {
        db[db_method](req.params.id).then(data => res.send(data));
    });
});

A
Anton Anton, 2017-02-13
@Fragster

Regular expressions can be used as a route. There is a .any() handler in which you can extract a specific type of request from req (get/post/delete/put). Well, there is app.route() , which can chain handlers into one chain (in combination with regular expressions). app.route().get().post().put().delete()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question