Last active
September 2, 2016 08:01
-
-
Save yarax/e68c76233f5600363fd8943a43afe480 to your computer and use it in GitHub Desktop.
This file contains 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
const http = require('http'); | |
// Swagger-like API description | |
const api = { | |
'/users': { | |
'get': { | |
'parameters': { | |
'role': { | |
'in': 'query' | |
}, | |
'session_id': { | |
'in': 'cookies' | |
} | |
}, | |
'x-handler': 'getUsers' | |
} | |
} | |
}; | |
function getUsers(role, session_id) { | |
return checkSession(session_id) | |
.then(getUserByRole(role)); | |
} | |
/** | |
* @param {Object} req node req object | |
* @return {Array} params | |
*/ | |
function prepareParamsBasedOnSchema(req) { | |
// processing request, including all possible HTTP cases | |
} | |
function getHandlerForRequest(req) { | |
// retrieving handler from current req based on schema | |
} | |
/** | |
* @param res node res object | |
* @returns {function()} | |
*/ | |
function formResponseBasedOnSchema(res) { | |
return (result) => { | |
// Filling headers according to schema | |
// Sending result | |
} | |
} | |
function errorHandler(res) { | |
return (error) => { | |
// log error and send some response | |
} | |
} | |
http.createServer((req, res) => { | |
const params = prepareParamsBasedOnSchema(req); | |
const handler = getHandlerForRequest(req); | |
handler.apply(null, params) | |
.then(formResponseBasedOnSchema(res)) | |
.catch(errorHandler(res)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment