diff --git a/388b_code_examples/input.txt b/388b_code_examples/input.txt new file mode 100644 index 0000000000000000000000000000000000000000..3a765d28d85bf4942681dff8015fd2821a276456 --- /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 0000000000000000000000000000000000000000..915e387b0ea060ab24015e74770e61bd38398747 --- /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 0000000000000000000000000000000000000000..ebab8f22079b2e9d2b9307c285217443a0721215 --- /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 0000000000000000000000000000000000000000..fb1e56a6573f8cf62f22f5bdca273652fe21a94b --- /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 0000000000000000000000000000000000000000..4cb722da3f95c43c5126e2ff1d049a255a47c6fe --- /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 0000000000000000000000000000000000000000..7d5e03464a5f246d0c4ea4cd34ca4b8972a5bbb6 --- /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