const express = require('express'); const bodyParser = require('body-parser'); const routes = { users: require('./routes/users'), pitch_types: require('./routes/pitchTypes'), // Add more routes here... // items: require('./routes/items'), }; const app = express(); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); // We create a wrapper to workaround async errors not being transmitted correctly. function makeHandlerAwareOfAsyncErrors(handler) { return async function(req, res, next) { try { await handler(req, res); } catch (error) { next(error); } }; } // We provide a root route just as an example app.get('/', (req, res) => { res.send(`

Hello, Sequelize + Express!

Make sure you have executed npm run setup-example-db once to have a populated example database. Otherwise, you will get 'no such table' errors.

Try some routes, such as /api/users or /api/orchestras?includeInstruments!

To experiment with POST/PUT/DELETE requests, use a tool for creating HTTP requests such as HTTPie, Postman, or even the curl command, or write some JS code for it with got, ky or axios.

`); }); // We define the standard REST APIs for each route (if they exist). for (const [routeName, routeController] of Object.entries(routes)) { if (routeController.getAll) { app.get( `/api/${routeName}`, makeHandlerAwareOfAsyncErrors(routeController.getAll) ); } if (routeController.getById) { app.get( `/api/${routeName}/:id`, makeHandlerAwareOfAsyncErrors(routeController.getById) ); } if (routeController.create) { app.post( `/api/${routeName}`, makeHandlerAwareOfAsyncErrors(routeController.create) ); } if (routeController.update) { app.put( `/api/${routeName}/:id`, makeHandlerAwareOfAsyncErrors(routeController.update) ); } if (routeController.remove) { app.delete( `/api/${routeName}/:id`, makeHandlerAwareOfAsyncErrors(routeController.remove) ); } } module.exports = app;