Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added app.svelte
Empty file.
Empty file added nested.svelte
Empty file.
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "",
"version": "",
"description": "",
"author": "",
"scripts": {
"start": "node server.improved.js"
},
"dependencies": {
"mime": "^2.4.4"
}
}
1 change: 1 addition & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*Style your own assignment! This is fun! */
41 changes: 41 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!doctype html>
<html lang="en">
<head>
<title>CS4241 Assignment 2</title>
<meta charset="utf-8">
</head>
<body>
<form action="">
<input type='text' id='yourname' value="your name here">
<button>submit</button>
</form>
</body>
<script>

const submit = function( e ) {
// prevent default form action from being carried out
e.preventDefault()

const input = document.querySelector( '#yourname' ),
json = { yourname: input.value },
body = JSON.stringify( json )

fetch( '/submit', {
method:'POST',
body
})
.then( function( response ) {
// do something with the reponse
console.log( response )
})

return false
}

window.onload = function() {
const button = document.querySelector( 'button' )
button.onclick = submit
}

</script>
</html>
3 changes: 3 additions & 0 deletions public/js/scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Add some Javascript code here, to run on the front end.

console.log("Welcome to assignment 2!")
72 changes: 72 additions & 0 deletions server.improved.js
Original file line number Diff line number Diff line change
@@ -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 )