package.json
{
  "name": "express-server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "mongoose": "^5.10.7"
  }
}
index.js
const mongoose = require('mongoose');
const express = require('express');
const bodyParser = require('body-parser');
const routes = require('./lib/routes');

let port = 6061;

mongoose.connect('mongodb://localhost:27017/companies', {useNewUrlParser: true, useUnifiedTopology: true});

let app = express();

app.use('/', bodyParser.json());

app.use('/v1/', routes);

app.listen(port, function() {
    console.log("Listening on :%d", port);
});
makeData.js
const mongoose = require('mongoose');
const Company = require('./lib/models/company');
const Employee = require('./lib/models/employee');

mongoose.connect('mongodb://localhost:27017/companies');

const innitech = new Company({
    name: "InnitechCorp",
    numberOfEmployees: 5
});

innitech.save().catch(err => console.error(err)).then(() => console.log('saved innitech'));

const peter = new Employee({
   name: "Peter",
   job: "Paper Pusher",
   rank: 5,
   company: innitech.name
});

peter.save().catch(err => console.error(err)).then(() => console.log('saved peter'));
lib/routes.js
const express = require('express');
const companies = require('./controllers/companies');
const employees = require('./controllers/employees');

let routes = express.Router();

routes.get('/companies', companies.root);

routes.route('/companies/:companyName')
    .get(companies.name)
    .put(companies.create)

routes.get('/companies/:companyName/employees', employees.employees)

routes.route('/companies/:companyName/employees/:employeeName')
    .get(employees.singleEmployee)
    .put(employees.createOrUpdateEmployee)
    .delete(employees.removeEmployee)

module.exports = routes;
lib/controllers/companies.js
const Company = require('../models/company');

module.exports = {
    root: (req, res) => {
        Company.find().exec((err, companies) => {
            if (err) {
                res.status(500);
                res.json(err);
                return;
            }
            res.json(companies);
        });
    },
    name: (req, res) => {
        let name = req.params.companyName;

        Company.findOne().byName(name).exec((err, company) => {
           if (err) {
               res.status(500);
               res.json(err);
               return;
           }
           if (!company)
               return res.sendStatus(404);

           res.json(company);
        });
    },
    create: (req, res) => {
        let name = req.params.companyName;
        let body = req.body;

        if (name !== body['name']) {
            return res.status(400).json({error: "name does not match company name"})
        }

        Company.findOne().byName(name)
            .then(company => {
                if (company) {
                    // update, not delete
                    for (let field in body)
                        company[field] = body[field];
                    company.save()
                        .then(() => { res.status(200).json(company) })
                        .catch((err) => { res.status(400).json(err) })
                    return;
                }

                let newCompany = Company(body);

                newCompany.save()
                    .then(() => { res.status(200).json(newCompany) })
                    .catch((err) => { res.status(400).json(err) });
            });
    }
}
lib/controllers/employees.js
const Company = require('../models/company');
const Employee = require('../models/employee');

module.exports = {
    employees: (req, res) => {
        let name = req.params.companyName;

        Employee.find().byCompany(name).exec((err, employees) => {
            if (err) {
                res.status(500);
                res.json(err);
                return;
            }

            if (employees && employees.length === 0) {
                res.status(404);
                res.json({err: 'not found'});
                return;
            }

            res.json(employees);
        });
    },
    singleEmployee: (req, res) => {
        let companyName = req.params.companyName;
        let employeeName = req.params.employeeName;

        Employee.findOne().byCompanyAndName(companyName, employeeName).exec((err, employee) => {
            if (err) {
                res.status(500);
                res.json(err);
                return;
            }

            if (!employee) {
                res.sendStatus(404);
                return;
            }

            res.json(employee);
        });
    },
    createOrUpdateEmployee: (req, res) => {
        let companyName = req.params.companyName;
        let employeeName = req.params.employeeName;
        let body = req.body;

        if (body['name'] !== employeeName) {
            res.status(400);
            res.json({error: "name does not match request employee name"});
            return;
        }

        if (body['company'] !== companyName) {
            res.status(400);
            res.json({error: "company does not match request employee company"});
            return;
        }

        Employee.findOne().byCompanyAndName(companyName, employeeName).exec()
            .then((doc) => {
                if (doc) {
                    for (let prop in body)
                        doc[prop] = body[prop];

                    doc.save()
                        .then(() => { res.status(200).json(doc) })
                        .catch(err => { res.status(400).json(err) });
                } else {
                    const newEmployee = new Employee(body);
                    newEmployee.save()
                        .then(() => { res.status(200).json(newEmployee) } )
                        .catch(err => { res.status(400).json(err) });
                }
            })
    },
    removeEmployee: (req, res) => {
        let companyName = req.params.companyName;
        let employeeName = req.params.employeeName;

        Employee.findOne()
            .byCompanyAndName(companyName, employeeName)
            .exec((err, doc) => {
                if (err) {
                    res.status(500);
                    res.json(err);
                    return;
                }
                doc.delete()
                    .then(() => { res.sendStatus(204) })
                    .catch((err) => {
                            res.status(500);
                            res.json(err);
                        });
            });
    },
}
lib/models/Employee.js
const mongoose = require('mongoose');

const EmployeeSchema = mongoose.Schema({
    name: String,
    job: String,
    rank: Number,
    company: String
});

EmployeeSchema.query.byCompany = function(name) {
    return this.where({company: name});
}

EmployeeSchema.query.byCompanyAndName = function(company, name) {
    return this.where({company:company, name: name});
}

const Employee = mongoose.model('Employee', EmployeeSchema);

module.exports = Employee;
lib/models/Company.js
const mongoose = require('mongoose');

const CompanySchema = mongoose.Schema({
    name: {
        type: String,
        required: true,
        unique: true
    },
    numberOfEmployees: Number
});

CompanySchema.query.byName = function(name) {
    return this.where({name: name});
}

const Company = mongoose.model('Company', CompanySchema);

module.exports = Company;