Routes in separate files

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);

Is it good to separate the routes?

    
asked by anonymous 31.10.2016 / 13:46

1 answer

2

You can separate the routes by file and is a good practice. An example below:

app.js

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

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

notes.js

var express = require("express");
var router = express.Router();

router.get("/", function(req, res) {
  res.setHeader("Content-Type", "application/json;charset=utf8");
  res.jsonp([]);
});

module.exports = router;
    
31.10.2016 / 13:56