Skip to content

Instantly share code, notes, and snippets.

@codenamejason
Created September 29, 2015 03:34
Show Gist options
  • Save codenamejason/0acc5bd096f9e4352474 to your computer and use it in GitHub Desktop.
Save codenamejason/0acc5bd096f9e4352474 to your computer and use it in GitHub Desktop.
// Define variables to access each service. Note the usage of
// node.js modules via the "require(...)" calls.
var services = {
countries: require("./countries_service"),
regions: require("./regions_service"),
cities: require("./cities_service")
}
function sendResults(res, params, status, statusText, headers, result) {
// Send a HTTP response, with the given "status" code and a
// "statusText" explanation, a "Connection:close" header so the
// connection won't be kept alive, a "Content-Type:application/json"
// header to specify the result type, some optional additional headers,
// and a result (if any) in JSON or JSONP format.
}
function routeCall(req, res, body) {
// Analyze the URL and the request body (if present) and the
// pathname itself to get query parameters. Check whether the result
// will be in JSON or JSONP format by seeing if a "callback" parameter
// is given.
//
// If a "_method" parameter was included, let it override the
// actual HTTP method.
//
// Analyze the URL to decide what service to call, and if
// present in the "services" array above, dispatch the call to
// it, with a callback pointing to the "sendResults" function;
// otherwise, send back a 404 error.
}
process.on('uncaughtException', function(err) {
// Provide for unexpected exceptions, so the server won't crash
// Usually, report the error.
})
require("http").createServer(function (req, res) {
// This is the main function, that actually acts as a server
// We'll listen on port 8888, just not to interfere with Apache
//
// For PUT/POST methods, wait until the complete request body
// has been read, and then call routeCall.
// For GET/DELETE methods, call routeCall directly
}
}).listen(8888)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment