Chapter 1: Introduction to the MEAN Stack
Activity 1: Creating an HTTP Server for a Blogging Application
Create a new folder at an appropriate location in your system and rename it Blogging Application.
Open the newly created Blogging Application folder from your code editor (Visual Studio) and create a server.js file, as shown in the following screenshot:
Declare and assign HTTP using the following command:
var http = require ('http');
Declare and assign the hostname and port number:
const hostname = '127.0.0.1'; const port = 8000;
Create the HTTP server:
const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Blogging Application\n');});
Call the server to listen to localhost on the specified port:
server.listen(port, hostname, () => { console.log ('Server running at http://${hostname}:${port}/');});
Run the server by first pressing Ctrl + to open the integrated command-line terminal in Visual Studio and then type the following:
>node server.js
You will obtain the following output:
Go to your browser and type http://localhost:3000 in the URL address bar. You should obtain the following output:
Activity 2: Streaming Data to a File
Go to your Desktop and create a new folder and rename it StreamingProgram.
Open the newly created StreamingPrgram folder from your code editor (Visual Studio) and create stream.js, readTextFile.txt, and writeTextFile.txt, as shown here:
Load and import the filesystem module into the stream.js file using the following code:
var fs = require('fs');
Create a readable stream by calling the createReadStream() function on the readTextFile.txt file using the following code:
Const readableStream = fs.createReadStream('readTextFile.txt');
Create a writable stream by calling the createWriteStream() function on the writeTextFile.txt file using the following code:
Const writableStream = fs.createWriteStream('writeTextFile.txt');
Call the on() function on the readableStream() method. Then, pass data as the first argument and then attach a callback, as shown in the following code:
readableStream.on('data', function (data) { console.log('Hey!, I am about to write what has been read from this file readTextFile.txt'); });
Call the write() method on the writeableStream() method to write data inside the readableStream() callback using the following code:
readableStream.on('data', function (data) { console.log('Hey!, am about to write what has been read from this file readTextFile.txt'); var txt = ' Written!' var newdata = data + txt; //Append text string to the read data if (writableStream.write(newdata) === true) { console.log('Hey!, am done writing, Open the file writeTextFile.txt to see what has been written'); } else console.log('Writing is not successful'); });
Press Ctrl+ to open the integrated command-line terminal in Visual Studio and run the program by typing in the following code:
node stream.js
You will obtain the following output:
Then, open the writeTextFile.txt file to confirm the text that was read and written into it.