From e09567ba9f7fd1ae3542e350a22f34773ab0acfa Mon Sep 17 00:00:00 2001
From: Andrej Rasevic <andrej@rasevicengineering.com>
Date: Fri, 4 Jan 2019 14:40:59 -0500
Subject: [PATCH] adding node code examples

---
 388b_code_examples/input.txt      |  3 +++
 388b_code_examples/main.js        | 15 ++++++++++++
 388b_code_examples/readFile.js    | 30 ++++++++++++++++++++++++
 388b_code_examples/web/client.js  | 25 ++++++++++++++++++++
 388b_code_examples/web/index.html |  9 ++++++++
 388b_code_examples/web/server.js  | 38 +++++++++++++++++++++++++++++++
 6 files changed, 120 insertions(+)
 create mode 100644 388b_code_examples/input.txt
 create mode 100644 388b_code_examples/main.js
 create mode 100644 388b_code_examples/readFile.js
 create mode 100644 388b_code_examples/web/client.js
 create mode 100644 388b_code_examples/web/index.html
 create mode 100644 388b_code_examples/web/server.js

diff --git a/388b_code_examples/input.txt b/388b_code_examples/input.txt
new file mode 100644
index 0000000..3a765d2
--- /dev/null
+++ b/388b_code_examples/input.txt
@@ -0,0 +1,3 @@
+This is some sample text to illustrate the
+difference between a synchronous and an asychronous file
+read in javascript.
\ No newline at end of file
diff --git a/388b_code_examples/main.js b/388b_code_examples/main.js
new file mode 100644
index 0000000..915e387
--- /dev/null
+++ b/388b_code_examples/main.js
@@ -0,0 +1,15 @@
+var fs = require("fs");
+
+// Asynchronous read
+fs.readFile('input.txt', function (err, data) {
+   if (err) {
+      return console.error(err);
+   }
+   console.log("Asynchronous read: " + data.toString());
+});
+
+// Synchronous read
+var data = fs.readFileSync('input.txt');
+console.log("Synchronous read: " + data.toString());
+
+console.log("Program Ended");
\ No newline at end of file
diff --git a/388b_code_examples/readFile.js b/388b_code_examples/readFile.js
new file mode 100644
index 0000000..ebab8f2
--- /dev/null
+++ b/388b_code_examples/readFile.js
@@ -0,0 +1,30 @@
+var fs = require("fs");
+var buf = new Buffer(1024);
+
+console.log("Going to open an existing file");
+fs.open('input.txt', 'r+', function(err, fd) {
+   if (err) {
+      return console.error(err);
+   }
+   console.log("File opened successfully!");
+   console.log("Going to read the file");
+   
+   fs.read(fd, buf, 0, buf.length, 0, function(err, bytes) {
+      if (err) {
+         console.log(err);
+      }
+
+      // Print only read bytes to avoid junk.
+      if(bytes > 0) {
+         console.log(buf.slice(0, bytes).toString());
+      }
+
+      // Close the opened file.
+      fs.close(fd, function(err) {
+         if (err) {
+            console.log(err);
+         } 
+         console.log("File closed successfully.");
+      });
+   });
+});
diff --git a/388b_code_examples/web/client.js b/388b_code_examples/web/client.js
new file mode 100644
index 0000000..fb1e56a
--- /dev/null
+++ b/388b_code_examples/web/client.js
@@ -0,0 +1,25 @@
+var http = require('http');
+
+// Options to be used by request 
+var options = {
+   host: 'localhost',
+   port: '8081',
+   path: '/index.html'  
+};
+
+// Callback function is used to deal with response
+var callback = function(response) {
+   // Continuously update stream with data
+   var body = '';
+   response.on('data', function(data) {
+      body += data;
+   });
+   
+   response.on('end', function() {
+      // Data received completely.
+      console.log(body);
+   });
+}
+// Make a request to the server
+var req = http.request(options, callback);
+req.end();
\ No newline at end of file
diff --git a/388b_code_examples/web/index.html b/388b_code_examples/web/index.html
new file mode 100644
index 0000000..4cb722d
--- /dev/null
+++ b/388b_code_examples/web/index.html
@@ -0,0 +1,9 @@
+<html>
+   <head>
+      <title>Test Page</title>
+   </head>
+   
+   <body>
+      Welcome to 388b winter 2019!!!
+   </body>
+</html>
\ No newline at end of file
diff --git a/388b_code_examples/web/server.js b/388b_code_examples/web/server.js
new file mode 100644
index 0000000..7d5e034
--- /dev/null
+++ b/388b_code_examples/web/server.js
@@ -0,0 +1,38 @@
+var http = require('http');
+var fs = require('fs');
+var url = require('url');
+
+// Create a server
+http.createServer( function (request, response) {  
+   // Parse the request containing file name
+   var pathname = url.parse(request.url).pathname;
+   console.log("Pathname is: " + pathname);
+   
+   // Print the name of the file for which request is made.
+   console.log("Request for " + pathname + " received.");
+   
+   // Read the requested file content from file system
+   fs.readFile(pathname.substr(1), function (err, data) {
+      if (err) {
+         console.log(err);
+         
+         // HTTP Status: 404 : NOT FOUND
+         // Content Type: text/plain
+         response.writeHead(404, {'Content-Type': 'text/html'});
+      } else {	
+         //Page found	  
+         // HTTP Status: 200 : OK
+         // Content Type: text/plain
+         response.writeHead(200, {'Content-Type': 'text/html'});	
+         
+         // Write the content of the file to response body
+         response.write(data.toString());		
+      }
+      
+      // Send the response body 
+      response.end();
+   });   
+}).listen(8081);
+
+// Console will print the message
+console.log('Server running at http://127.0.0.1:8081/');
\ No newline at end of file
-- 
GitLab