Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
MongoDB, Express, Angular, and Node.js Fundamentals

You're reading from   MongoDB, Express, Angular, and Node.js Fundamentals Become a MEAN master and rule the world of web applications

Arrow left icon
Product type Paperback
Published in Mar 2019
Publisher
ISBN-13 9781789808735
Length 362 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Paul Oluyege Paul Oluyege
Author Profile Icon Paul Oluyege
Paul Oluyege
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

MongoDB, Express, Angular, and Node.js Fundamentals
Preface
1. Introduction to the MEAN Stack FREE CHAPTER 2. Developing RESTful APIs to Perform CRUD Operations 3. Beginning Frontend Development with Angular CLI 4. The MEAN Stack Security 5. Angular Declarables, Bootstrapping, and Modularity 6. Testing and Optimizing Angular Applications Appendix

Chapter 1: Introduction to the MEAN Stack


Activity 1: Creating an HTTP Server for a Blogging Application

  1. 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:

    Figure 1.19: Creating the server.js file

  2. Declare and assign HTTP using the following command:

    var http = require ('http'); 
  3. Declare and assign the hostname and port number:

    const hostname = '127.0.0.1';
    const port = 8000; 
  4. Create the HTTP server:

    const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Blogging Application\n');});
  5. Call the server to listen to localhost on the specified port:

    server.listen(port, hostname, () => {
    console.log ('Server running at
    http://${hostname}:${port}/');});
  6. 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:

    Figure 1.20: Running the server on the command-line interface

  7. Go to your browser and type http://localhost:3000 in the URL address bar. You should obtain the following output:

    Figure 1.21: Final output of the activity

Activity 2: Streaming Data to a File

  1. Go to your Desktop and create a new folder and rename it StreamingProgram.

  2. Open the newly created StreamingPrgram folder from your code editor (Visual Studio) and create stream.js, readTextFile.txt, and writeTextFile.txt, as shown here:

    Figure 1.22: Creating the streaming program files

  3. Load and import the filesystem module into the stream.js file using the following code:

    var fs = require('fs');
  4. Create a readable stream by calling the createReadStream() function on the readTextFile.txt file using the following code:

    Const readableStream = fs.createReadStream('readTextFile.txt');
  5. Create a writable stream by calling the createWriteStream() function on the writeTextFile.txt file using the following code:

    Const writableStream = fs.createWriteStream('writeTextFile.txt');
  6. 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');
    });
  7. 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');
    });
  8. 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:

    Figure 1.23: Running the streaming program

    Then, open the writeTextFile.txt file to confirm the text that was read and written into it.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image