Application
modularity

node-example-app

A boilerplate

express.js

http middelware


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

app.get('/boats', (req, res, next) => {
    res.status(200).send('Loads of boats');
});

app.get('/boats/:id', (req, res, next) => {
    res.status(200).send(`Boat with ID ${req.params.id}`);
});

app.get('/realestate', (req, res, next) => {
    res.status(200).send('Plenty of real estate');
});

app.get('/realestate/:id', (req, res, next) => {
    res.status(200).send(`Real estate with ID ${req.params.id}`);
});

app.listen(8080);

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

app.get('/boats', (req, res, next) => {
    res.status(200).send('Loads of boats');
});

app.get('/boats/:id', (req, res, next) => {
    res.status(200).send(`Boat with ID ${req.params.id}`);
});

app.get('/realestate', (req, res, next) => {
    res.status(200).send('Plenty of real estate');
});

app.get('/realestate/:id', (req, res, next) => {
    res.status(200).send(`Real estate with ID ${req.params.id}`);
});

app.use((req, res) => {
    res.status(404).send('Not found');
});

app.listen(8080);

next(new Error());


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

app.get('/boats', (req, res, next) => {
    res.status(200).send('Loads of boats');
});

app.get('/boats/:id', (req, res, next) => {
    if (req.params.id > 10000) return next(boom.badRequest('Bad request - Validation error'));
    res.status(200).send(`Boat with ID ${req.params.id}`);
});

app.get('/realestate', (req, res, next) => {
    res.status(200).send('Plenty of real estate');
});

app.get('/realestate/:id', (req, res, next) => {
    res.status(200).send(`Real estate with ID ${req.params.id}`);
});

app.use((error, req, res, next) => {
    if (error.isBoom) return res.status(error.output.statusCode).send(error.message);
    next(error);
});

app.use((req, res) => {
    res.status(404).send('Not found');
});

app.listen(8080);

Middelware

app.use();


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

app.use((req, res, next) => {
    if (req.params.id > 10000) return next(boom.badRequest('Bad request - Validation error'));
    next();
});

app.get('/boats', (req, res, next) => {
    res.status(200).send('Loads of boats');
});

app.get('/boats/:id', (req, res, next) => {
    res.status(200).send(`Boat with ID ${req.params.id}`);
});

app.get('/realestate', (req, res, next) => {
    res.status(200).send('Plenty of real estate');
});

app.get('/realestate/:id', (req, res, next) => {
    res.status(200).send(`Real estate with ID ${req.params.id}`);
});

app.use((error, req, res, next) => {
    if (error.isBoom) return res.status(error.output.statusCode).send(error.message);
    next(error);
});

app.use((req, res) => {
    res.status(404).send('Not found');
});

app.listen(8080);

express.Router();


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

app.use((req, res, next) => {
    if (req.params.id > 10000) return next(boom.badRequest('Bad request - Validation error'));
    next();
});

const boats = express.Router();
boats.get('/boats', (req, res, next) => {
    res.status(200).send('Loads of boats');
});
boats.get('/boats/:id', (req, res, next) => {
    res.status(200).send(`Boat with ID ${req.params.id}`);
});
app.use(boats);

const realestate = express.Router();
realestate.get('/realestate', (req, res, next) => {
    res.status(200).send('Plenty of real estate');
});
realestate.get('/realestate/:id', (req, res, next) => {
    res.status(200).send(`Real estate with ID ${req.params.id}`);
});
app.use(realestate);

app.use((error, req, res, next) => {
    if (error.isBoom) return res.status(error.output.statusCode).send(error.message);
    next(error);
});

app.use((req, res) => {
    res.status(404).send('Not found');
});

app.listen(8080);

npm modules


// Validation module

module.exports = class Validation {
    constructor (options = {}) {
        this.options = options;
    }
    middleware() {
        return (req, res, next) => {
            if (req.params.id > 10000) return next(boom.badRequest('Bad request - Validation error'));
            next();
        }
    }
}


// Error handler module

module.exports = class HttpErrors {
    constructor (options = {}) {
        this.options = options;
    }
    middleware() {
        return (error, req, res, next) => {
            if (error.isBoom) return res.status(error.output.statusCode).send(error.message);
            next(error);
        }
    }
    views() {
        return path.resolve(__dirname, '../views/');
    }
}

// Boats module
module.exports = class Boats {
    constructor () {
        this.app = express.Router();
        this.app.get('/boats', (req, res, next) => {
            res.status(200).send('Loads of boats');
        });
        this.app.get('/boats/:id', (req, res, next) => {
            res.status(200).send(`Boat with ID ${req.params.id}`);
        });
    }
    router() {
        return this.app;
    }
    views() {
        return path.resolve(__dirname, '../views/');
    }
}

// Realestate module
module.exports = class Realestate {
    constructor () {
        this.app = express.Router();
        this.app.get('/realestate', (req, res, next) => {
            res.status(200).send('Plenty of real estate');
        });
        this.app.get('/realestate/:id', (req, res, next) => {
            res.status(200).send(`Real estate with ID ${req.params.id}`);
        });
    }
    router() {
        return this.app;
    }
    views() {
        return path.resolve(__dirname, '../views/');
    }
}

Fleksibility


const express = require('express');
const Validation = require('Validation');
const HttpErrors = require('HttpErrors');
const Boats = require('Boats');
const Realestate = require('Realestate');

const validation = new Validation();
const httpErrors = new HttpErrors();
const boats = new Boats();
const realestate = new Realestate();

const app = express();

app.set('view engine', 'hbs');
app.set('views', [
    boats.views(),
    realestate.views(),
    httpErrors.views()
]);

app.use(validation.middelware());
app.use(boats.router());
app.use(realestate.router());
app.use(httpErrors.middelware());

app.listen(8080);

const express = require('express');
const Validation = require('Validation');
const HttpErrors = require('HttpErrors');
const Boats = require('Boats');
const Cars = require('Cars');
const Realestate = require('Realestate');

const validation = new Validation();
const httpErrors = new HttpErrors();
const boats = new Boats();
const cars = new Cars();
const realestate = new Realestate();

const app = express();

app.set('view engine', 'hbs');
app.set('views', [
    boats.views(),
    cars.views(),
    realestate.views(),
    httpErrors.views()
]);

app.use(validation.middelware());
app.use(boats.router());
app.use(cars.router());
app.use(realestate.router());
app.use(httpErrors.middelware());

app.listen(8080);
const express = require('express');
const Validation = require('Validation');
const HttpErrors = require('HttpErrors');
const Boats = require('Boats');

const validation = new Validation();
const httpErrors = new HttpErrors();
const boats = new Boats();
const app = express();

app.set('view engine', 'hbs');
app.set('views', [
    boats.views(),
    httpErrors.views()
]);

app.use(validation.middelware());
app.use(boats.router());
app.use(httpErrors.middelware());
const express = require('express');
const Validation = require('Validation');
const HttpErrors = require('HttpErrors');
const Cars = require('Cars');

const validation = new Validation();
const httpErrors = new HttpErrors();
const cars = new Cars();
const app = express();

app.set('view engine', 'hbs');
app.set('views', [
    cars.views(),
    httpErrors.views()
]);

app.use(validation.middelware());
app.use(cars.router());
app.use(httpErrors.middelware());
const express = require('express');
const Validation = require('Validation');
const HttpErrors = require('HttpErrors');
const Realestate = require('Realestate');

const validation = new Validation();
const httpErrors = new HttpErrors();
const realestate = new Realestate();
const app = express();

app.set('view engine', 'hbs');
app.set('views', [
    realestate.views(),
    httpErrors.views()
]);

app.use(validation.middelware());
app.use(realestate.router());
app.use(httpErrors.middelware());

const express = require('express');
const Validation = require('Validation');
const HttpErrors = require('HttpErrors');
const Boats = require('Boats');
const Realestate = require('Realestate');

const errorPug = require('errorPugTemplates');
const boatsPug = require('boatsPugTemplates');
const realestatePug = require('realestatePugTemplates');

const validation = new Validation();
const httpErrors = new HttpErrors();
const boats = new Boats();
const realestate = new Realestate();

const app = express();

app.set('view engine', 'pug');
app.set('views', [
    boatsPug.views(),
    realestatePug.views(),
    errorPug.views()
]);

app.use(validation.middelware());
app.use(boats.router());
app.use(realestate.router());
app.use(httpErrors.middelware());

app.listen(8080);
  • Config
  • Logging
  • Metrics
  • Security
  • Circuit breaking
  • Error handling
  • Crash recovery

Thank you