Created
November 12, 2016 18:18
-
-
Save NickNaso/7a74b9072281fbaed9422ac3e7072b77 to your computer and use it in GitHub Desktop.
Dinamically attach routes to express app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const express = require('express') | |
const http = require('http') | |
const app = express() | |
function handleGet (req, res) { | |
res.send('/GET handler') | |
} | |
function handlePost (req, res) { | |
res.send('/POST handler') | |
} | |
function handlePut (req, res) { | |
res.send('/PUT handler') | |
} | |
function handleDelete (req, res) { | |
res.send('/DELETE handler') | |
} | |
const routes = [ | |
{ | |
path: "/", | |
method: "get", | |
handler: handleGet | |
}, | |
{ | |
path: "/", | |
method: "post", | |
handler: handlePost | |
}, | |
{ | |
path: "/", | |
method: "put", | |
handler: handlePut | |
}, | |
{ | |
path: "/", | |
method: "delete", | |
handler: handleDelete | |
}, | |
] | |
for (let r of routes) { | |
app[r.method](r.path, r.handler) | |
} | |
http.createServer(app, '0.0.0.0').listen(5000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment