Skip to content

Instantly share code, notes, and snippets.

@NickNaso
Created November 12, 2016 18:18
Show Gist options
  • Save NickNaso/7a74b9072281fbaed9422ac3e7072b77 to your computer and use it in GitHub Desktop.
Save NickNaso/7a74b9072281fbaed9422ac3e7072b77 to your computer and use it in GitHub Desktop.
Dinamically attach routes to express app
'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