Skip to content

Instantly share code, notes, and snippets.

@adamauckland
Last active March 31, 2017 14:53
Show Gist options
  • Select an option

  • Save adamauckland/586cb326d2fd010250482bb0d34cde3a to your computer and use it in GitHub Desktop.

Select an option

Save adamauckland/586cb326d2fd010250482bb0d34cde3a to your computer and use it in GitHub Desktop.

Revisions

  1. adamauckland revised this gist Mar 31, 2017. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions config.json
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    [
    {
    'path': '/todo/list',
    'handler': 'ListHandler',
    'verb': 'get
    "path": "/todo/list",
    "handler": "ListHandler",
    "verb": "get"
    },
    {
    'path': '/todo/list/:id',
    'handler': 'DetailHandler',
    'verb': 'get'
    "path": "/todo/list/:id",
    "handler": "DetailHandler",
    "verb": "get"
    }
    ]
  2. adamauckland revised this gist Mar 31, 2017. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions config.json
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    [
    {
    path: '/todo/list',
    handler: 'ListHandler',
    verb: 'get
    'path': '/todo/list',
    'handler': 'ListHandler',
    'verb': 'get
    },
    {
    path: '/todo/list/:id',
    handler: 'DetailHandler',
    verb: 'get'
    'path': '/todo/list/:id',
    'handler': 'DetailHandler',
    'verb': 'get'
    }
    ]
  3. adamauckland created this gist Mar 31, 2017.
    12 changes: 12 additions & 0 deletions config.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    [
    {
    path: '/todo/list',
    handler: 'ListHandler',
    verb: 'get
    },
    {
    path: '/todo/list/:id',
    handler: 'DetailHandler',
    verb: 'get'
    }
    ]
    19 changes: 19 additions & 0 deletions dynamic-express-app.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    var express = require('express');
    var app = express();

    // load config from file
    var config = require('config.json');

    // handler mappings
    var handlers = {
    'ListHandler': require('list-handler'), // different request handlers in different modules
    'DetailHandler': require('detail-handler')
    };

    config.forEach((route) => {
    // lookup the request handler from the handler mapping above so different paths can call different handlers.
    let routeHandler = handlers[route.handler];

    // now set up a route at the path defined in the config pointing to the handler
    app[route.verb](route.path, routeHandler);
    });