Created
August 29, 2018 22:38
-
-
Save mallond/c056ecbed3d2106be447e4eb8e14efcc to your computer and use it in GitHub Desktop.
Simple No Modules pure Node Example
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
/* | |
* Primary file for API | |
* | |
*/ | |
// Dependencies | |
var http = require('http'); | |
var url = require('url'); | |
var StringDecoder = require('string_decoder').StringDecoder; | |
// Configure the server to respond to all requests with a string | |
var server = http.createServer(function(req,res){ | |
// Parse the url | |
var parsedUrl = url.parse(req.url, true); | |
// Get the path | |
var path = parsedUrl.pathname; | |
var trimmedPath = path.replace(/^\/+|\/+$/g, ''); | |
// Get the query string as an object | |
var queryStringObject = parsedUrl.query; | |
// Get the HTTP method | |
var method = req.method.toLowerCase(); | |
//Get the headers as an object | |
var headers = req.headers; | |
// Get the payload,if any | |
var decoder = new StringDecoder('utf-8'); | |
var buffer = ''; | |
req.on('data', function(data) { | |
buffer += decoder.write(data); | |
}); | |
req.on('end', function() { | |
buffer += decoder.end(); | |
// Check the router for a matching path for a handler. If one is not found, use the notFound handler instead. | |
var chosenHandler = typeof(router[trimmedPath]) !== 'undefined' ? router[trimmedPath] : handlers.notFound; | |
// Construct the data object to send to the handler | |
var data = { | |
'trimmedPath' : trimmedPath, | |
'queryStringObject' : queryStringObject, | |
'method' : method, | |
'headers' : headers, | |
'payload' : buffer | |
}; | |
// Route the request to the handler specified in the router | |
chosenHandler(data,function(statusCode,payload){ | |
// Use the status code returned from the handler, or set the default status code to 200 | |
statusCode = typeof(statusCode) == 'number' ? statusCode : 200; | |
// Use the payload returned from the handler, or set the default payload to an empty object | |
payload = typeof(payload) == 'object'? payload : {}; | |
// Convert the payload to a string | |
var payloadString = JSON.stringify(payload); | |
// Return the response | |
res.setHeader('Content-Type', 'application/json'); | |
res.writeHead(statusCode); | |
res.end(payloadString); | |
console.log("Returning this response: ",statusCode,payloadString); | |
}); | |
}); | |
}); | |
// Start the server | |
server.listen(3000,function(){ | |
console.log('The server is up and running now'); | |
}); | |
// Define all the handlers | |
var handlers = {}; | |
// Sample handler | |
handlers.sample = function(data,callback){ | |
callback(406,{'name':'sample handler'}); | |
}; | |
// Not found handler | |
handlers.notFound = function(data,callback){ | |
callback(404); | |
}; | |
// Define the request router | |
var router = { | |
'sample' : handlers.sample | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment