diff --git a/app.svelte b/app.svelte new file mode 100644 index 000000000..e69de29bb diff --git a/nested.svelte b/nested.svelte new file mode 100644 index 000000000..e69de29bb diff --git a/package.json b/package.json new file mode 100644 index 000000000..988f135f7 --- /dev/null +++ b/package.json @@ -0,0 +1,12 @@ +{ + "name": "", + "version": "", + "description": "", + "author": "", + "scripts": { + "start": "node server.improved.js" + }, + "dependencies": { + "mime": "^2.4.4" + } +} diff --git a/public/css/style.css b/public/css/style.css new file mode 100644 index 000000000..d5f842ab8 --- /dev/null +++ b/public/css/style.css @@ -0,0 +1 @@ +/*Style your own assignment! This is fun! */ \ No newline at end of file diff --git a/public/index.html b/public/index.html new file mode 100644 index 000000000..c56d620eb --- /dev/null +++ b/public/index.html @@ -0,0 +1,41 @@ + + + + CS4241 Assignment 2 + + + +
+ + +
+ + + diff --git a/public/js/scripts.js b/public/js/scripts.js new file mode 100644 index 000000000..de052eae6 --- /dev/null +++ b/public/js/scripts.js @@ -0,0 +1,3 @@ +// Add some Javascript code here, to run on the front end. + +console.log("Welcome to assignment 2!") \ No newline at end of file diff --git a/server.improved.js b/server.improved.js new file mode 100644 index 000000000..26673fc09 --- /dev/null +++ b/server.improved.js @@ -0,0 +1,72 @@ +const http = require( 'http' ), + fs = require( 'fs' ), + // IMPORTANT: you must run `npm install` in the directory for this assignment + // to install the mime library used in the following line of code + mime = require( 'mime' ), + dir = 'public/', + port = 3000 + +const appdata = [ + { 'model': 'toyota', 'year': 1999, 'mpg': 23 }, + { 'model': 'honda', 'year': 2004, 'mpg': 30 }, + { 'model': 'ford', 'year': 1987, 'mpg': 14} +] + +const server = http.createServer( function( request,response ) { + if( request.method === 'GET' ) { + handleGet( request, response ) + }else if( request.method === 'POST' ){ + handlePost( request, response ) + } +}) + +const handleGet = function( request, response ) { + const filename = dir + request.url.slice( 1 ) + + if( request.url === '/' ) { + sendFile( response, 'public/index.html' ) + }else{ + sendFile( response, filename ) + } +} + +const handlePost = function( request, response ) { + let dataString = '' + + request.on( 'data', function( data ) { + dataString += data + }) + + request.on( 'end', function() { + console.log( JSON.parse( dataString ) ) + + // ... do something with the data here!!! + + response.writeHead( 200, "OK", {'Content-Type': 'text/plain' }) + response.end() + }) +} + +const sendFile = function( response, filename ) { + const type = mime.getType( filename ) + + fs.readFile( filename, function( err, content ) { + + // if the error = null, then we've loaded the file successfully + if( err === null ) { + + // status code: https://httpstatuses.com + response.writeHeader( 200, { 'Content-Type': type }) + response.end( content ) + + }else{ + + // file not found, error code 404 + response.writeHeader( 404 ) + response.end( '404 Error: File Not Found' ) + + } + }) +} + +server.listen( process.env.PORT || port )