Routes in separate files

Asked

Viewed 1,698 times

3

How do I put routes in separate files?

var express = require('express');
var App = express.Router();
var Notas = require('../api/notas');

App.route('/notas')
    .get(Notas.read)
    .post(Notas.create);

App.route('/notas/:id')
    .get(Notas.profile)
    .put(Notas.update)
    .delete(Notas.delete);

It is good to separate the routes?

1 answer

2


You can separate the routes by file. To do this create a new Router in the new archive and refer to your main route archive:

app js.

const express = require('express');
const app = express.Router();
const notas = require('../api/notas');

app.use('/notas', notas);

js.

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

const get = async (req, res) => {
  res.jsonp({ mensagem: 'OK' });
}

router.get('/', get);

module.exports = router;

Router

The router Object is an Isolated instance of middleware and Routes. You can think of it as a "mini-application", capable only of Performing middleware and routing functions. Every Express application has a built-in app router.

In free translation:

A router object is an isolated instance of middleware and routes. You can think of it as a "mini application", capable only of performing middleware function and routing. Every Express app has a built-in app router.

Browser other questions tagged

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