Skip to content

Latest commit

 

History

History
79 lines (62 loc) · 1.69 KB

section3.3.md

File metadata and controls

79 lines (62 loc) · 1.69 KB

Section 3.3: Modular express application

To make express web application modular use router factories:

Example-1

// Module: (greet.js)
let express = require('express');

module.exports = function(options = {}) { // Router factory
  let router = express.Router();
  router.get('/greet', (req, res, next) => {
    res.end(options.greeting);
  });
  return router;
};

// Application: (app.js)
let express = require('express');
let greetMiddleware = require('./greet.js');

express()
  .use('/api/v1/', greetMiddleware({ greeting:'Hi Morol' }))
  .listen(3000);

//  This will make your application modular, customisable and your code reusable.

Example-2

// Module: (greet.js)
const express = require('express');

module.exports = function(options = {}) { // Router factory
  const router = express.Router();
  // Get controller
  const {service} = options;
  router.get('/greet', (req, res, next) => {
    res.end(
      service.createGreeting(req.query.name || 'Stranger')
    );
  });
  return router;
};

// Application: (app.js)
const express = require('express');
const greetMiddleware = require('./greet.js');

class GreetingService {
  constructor(greeting = 'Hello') {
    this.greeting = greeting;
  }
  createGreeting(name) {
    return `${this.greeting}, ${name}!`;
  }
}

express()
  .use('/api/v1/service1', greetMiddleware({
    service: new GreetingService('Hello'),
  }))
  .use('/api/v1/service2', greetMiddleware({
    service: new GreetingService('Hi'),
  }))
  .listen(8080);