boolean-uk / software-developer

0 stars 0 forks source link

Decouple API route/controller functions from the app & app runner to prepare for CI #129

Open vherus opened 1 year ago

vherus commented 1 year ago

This repo has a complete decoupling with API specs for student requirements and CI for core criteria, extensions, and a separate main branch workflow: https://github.com/boolean-uk/database-cinema-booking-api

All of the API exercises from Address Book onwards needs this complete setup, starting with decoupling the app, app runner and controller functions: https://www.notion.so/boolean/Intro-A-Simple-API-0d717328f3eb415c9ac0732774de554c

E.g.:

server.js - app

const express = require('express');
const app = express();

const cors = require('cors');
const morgan = require('morgan');

app.disable('x-powered-by');

app.use(cors());
app.use(morgan('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// Controllers & routes extracted to their own files
const customerRouter = require('./routers/customer');
app.use('/customers', customerRouter);

module.exports = app

index.js - app runner

require('dotenv').config();
const app = require('./server.js')
const port = process.env.PORT || 4040;

app.listen(port, () => {
    console.log(`\n Server is running on http://localhost:${port}\n`);
});