diff --git a/assignment-1/config.js b/assignment-1/config.js new file mode 100644 index 0000000..3e242bf --- /dev/null +++ b/assignment-1/config.js @@ -0,0 +1,30 @@ +/* + * Create and export configuration variables + * + */ + +// Container for all environments +const environments = {}; + +// Staging (default) environment +environments.staging = { + 'httpPort' : 3000, + 'httpsPort' : 3001, + 'envName' : 'staging' +}; + +// Production environment +environments.production = { + 'httpPort' : 5000, + 'httpsPort' : 5001, + 'envName' : 'production' +}; + +// Determine which environment was passed as a command-line argument +const currentEnvironment = typeof(process.env.NODE_ENV) == 'string' ? process.env.NODE_ENV.toLowerCase() : ''; + +// Check that the current environment is one of the environments above, if not default to staging +const environmentToExport = typeof(environments[currentEnvironment]) == 'object' ? environments[currentEnvironment] : environments.staging; + +// Export the module +module.exports = environmentToExport; diff --git a/assignment-1/https/cert.pem b/assignment-1/https/cert.pem new file mode 100644 index 0000000..dd4220a --- /dev/null +++ b/assignment-1/https/cert.pem @@ -0,0 +1,24 @@ +-----BEGIN CERTIFICATE----- +MIID+DCCAuCgAwIBAgIJAJMvBZ7km7moMA0GCSqGSIb3DQEBCwUAMIGQMQswCQYD +VQQGEwJVUzETMBEGA1UECAwKY2FsaWZvcm5pYTEWMBQGA1UEBwwNU2FuIEZyYW5j +aXNjbzEPMA0GA1UECgwGcGlycGxlMQ8wDQYDVQQLDAZwaXJwbGUxEjAQBgNVBAMM +CWxvY2FsaG9zdDEeMBwGCSqGSIb3DQEJARYPb3BxcnNAZ21haWwuY29tMB4XDTE4 +MTIwODExNDczN1oXDTI4MTIwNTExNDczN1owgZAxCzAJBgNVBAYTAlVTMRMwEQYD +VQQIDApjYWxpZm9ybmlhMRYwFAYDVQQHDA1TYW4gRnJhbmNpc2NvMQ8wDQYDVQQK +DAZwaXJwbGUxDzANBgNVBAsMBnBpcnBsZTESMBAGA1UEAwwJbG9jYWxob3N0MR4w +HAYJKoZIhvcNAQkBFg9vcHFyc0BnbWFpbC5jb20wggEiMA0GCSqGSIb3DQEBAQUA +A4IBDwAwggEKAoIBAQD10Q1Syh8vUVN+ELL20UyqFTo4iqNXsYCmocWDrlHTJkO4 +xNkLOJprlSdxQkDoWpKIcvY9dQJHbSu81qIy13nrZ57NuEEcfpyyYpG93cVHhJk3 +O1JMgGUmY2heXHeeU4+TGwOB9Wykz9cj0uosF6B8iKb/5kNKOMvnUoASk6YHmjZ7 +YHtfVsxcLtJnx7Oc44v42WVnomUv03wfGdJn+Dq9PwF+9yXajg0i2himop/Uo9wz +YjW1pYIGab/uAxU6ikv06scZhH8l88bWH8LRgT3xEbeyqhcOUdYtGP+jAJlMK+dx +9kszxLZB2zQr9ooSSVGI3VSgIWR1FEZNfgCP6+vFAgMBAAGjUzBRMB0GA1UdDgQW +BBS75wHPs+gRWepdqzyivsD67g+bJDAfBgNVHSMEGDAWgBS75wHPs+gRWepdqzyi +vsD67g+bJDAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQCmISx9 +/WHZsC22vIDo6mNJUKeMqHp2qVGz16sGmCW2388z/xVWhM/ODxjgcg/IBvDMfXtM +spLt/voykTyk59qMM+QgTgjsfd3aSUB6hEaFIJxHevI9A0G5NJFFlyUsq6hY5ugx +n+LuOPTyLBrnK+W7ICxtYm3PTA+TlqUI1klbknhpaj4JQaXBAKPC4IGzA9qag3ii +wC2vdsB+mSwSNuhU8ku9eadDskGM3DBkNac8FSym7fzuZEMvzzGN9aPUI6RegLDb +RfMBJ2EjN3aMpsP7aG4WfY23MEYfZZhEF6JEakyBYFa0vtlJvVVoxgNSMnPO9iHW +3zRy2K4Ebxjg6Icq +-----END CERTIFICATE----- diff --git a/assignment-1/https/key.pem b/assignment-1/https/key.pem new file mode 100644 index 0000000..dfe2d95 --- /dev/null +++ b/assignment-1/https/key.pem @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQD10Q1Syh8vUVN+ +ELL20UyqFTo4iqNXsYCmocWD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ybdz9B6tfRLa8 +gKCxig+d8/Ufd8NpIVy2raXSOyEnQriYAFPg30hsgveJm7EuZLXm4sU+giotkqay +2velidFYqQrLc0o65fy0U6irwh0Ud8SNv4RvZKhmQv+bhpjCVIrKYKA46skCgYB0 +9j/rVZ4BvGapa5FfvDibXR3Kl4A4IxqG/nPSvb8HEIIlfycOQJPPv1OpLG7Y6Hsp +0aS6NNmdxgNn8KQc7/xhrrrsK3vPiWuUxnGVv2UY45N0xsCfFElrSmaUHS2j6/zh +wWHwH58ncsn8+OIRjbWfiii0EIHUq16GHqCnnlFvJQKBgC4erKAQyLf2a9zVpKhs +T4WcPYvu76PSHrNF2d+LqnEqnSZDEYuB/olIWYzkUgN1/E11gxwo+CGiP71JrtlL +uJrinuDOSvEVv8H8ZKTuOMXRVD4PSpC0wMMJR6uPH1ce8+KHyzcLzcV+6OoiZDLu +z3U9pX/nq0yUlLGYKaHSUczC +-----END PRIVATE KEY----- diff --git a/assignment-1/index.js b/assignment-1/index.js new file mode 100644 index 0000000..969bbc4 --- /dev/null +++ b/assignment-1/index.js @@ -0,0 +1,112 @@ +/* + Homework assignment #1 +*/ + + +// import all dependencies + +const http = require('http'); +const https = require('https'); +const url = require('url' ); +const { StringDecoder } = require('string_decoder'); +const fs = require('fs'); +const config = require('./config'); + +// create a http server and start it +http.createServer((req , res) => { + unifiedServer(req ,res); +}) +.listen(config.httpPort, () => console.log("http Server listening on port " + config.httpPort)); + + +let httpsServerOptions = { + 'key': fs.readFileSync('./https/key.pem'), + 'cert': fs.readFileSync('./https/cert.pem') +}; + +// create a https server and start it +https.createServer(httpsServerOptions,(req,res) =>{ + unifiedServer(req,res); +}) +.listen(config.httpsPort,()=>{ + console.log("https Server listening on port "+config.httpsPort); +}); + + +//create a unifiedServer +const unifiedServer = (req ,res) => { + + // get headers and method from request object + let { headers , method } = req ; + + //parse the url from request object + let parsedUrl = url.parse(req.url , true); + + //get query and pathname from parsedUrl + let { pathname , query } = parsedUrl; + + //trim the pathname using regex + let trimmedPath = pathname.replace(/^\/+|\/+$/g, ''); + + + //get payload from request , if any + let decoder = new StringDecoder('utf-8'); + + let buffer = ''; + req.on('data' , (data) => buffer += decoder.write(data)); + req.on('end' , () => { + buffer += decoder.end(); + + // Construct the data object to send to the handler + let data = { + 'trimmedPath' : trimmedPath, + 'queryStringObject' : query, + 'method' : method.toLowerCase(), + 'headers' : headers, + 'payload' : buffer + }; + + //choose a handler from router + let chosenHandler = router[trimmedPath] ? router[trimmedPath] : handlers.notFound; + + //route the request to chosen handler + chosenHandler(data, (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 + let payloadString = JSON.stringify(payload); + + // Return the response + res.writeHead(statusCode,{'Content-Type': 'application/json'}); + res.end(payloadString); + console.log("Returning this response: ",statusCode,payloadString); + + }); + + + }); + +}; + + +//define handlers +const handlers = {}; + +//hello handler +handlers.hello = (data , callback) => { + callback(300 , {'welcomeMessage': 'welcome to my assignment #1'}); +} +//not found handler , returns empty object +handlers.notFound = (data , callback) => { + callback(404); +} + +//define router + +const router = { + 'hello' : handlers.hello +}