With this approach, there is no need to write routes manually. Just set up a directory structure such as URL paths. An example route is located in /routes/user/table/table.get.js , and the API route will be /user/table .
import app from './app' import fs from 'fs-readdir-recursive' import each from 'lodash/each' import nth from 'lodash/nth' import join from 'lodash/join' import initial from 'lodash/initial' const routes = fs(`${__dirname}/routes`) each(routes, route => { let paths = route.split('/')
Route example:
import { Router } from 'express' import Table from '@models/table.model' const routes = Router() routes.get('/', (req, res, next) => { Table .find({user: userIdentifier}) .select('-user') .lean() .then(table => res.json(table)) .catch(error => next(error)) }) module.exports = routes
source share