Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1777

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T11:19:08+00:00 2024-11-25T11:19:08+00:00

Day 1 – Your first week in NodeJS

  • 62k

I believe you know something about Node.js before starting on this course, just to refresh your memory. Node.js is a JavaScript runtime, built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking model that makes it lightweight and efficient.

Node.js package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

Let's jump on Day 1 agenda now.

1. Installation

You can simply go to official Node.js website and download the latest stable version (LTS) based on your OS.

image.png

After the installation, you can simply open your terminal/command prompt, to check if it's running or not.

$ node --version // v14.16.1 (in my case) 
Enter fullscreen mode Exit fullscreen mode

In addition to the above, you can also go to any editor and create a file app.js (or any name).

// app.js console.log("node is working"); 
Enter fullscreen mode Exit fullscreen mode

Now run this file using Node.js

$ node app.js // node is working 
Enter fullscreen mode Exit fullscreen mode

You have installed Node.js on your machine 🏆

2. Global Objects

After installing Node.js, let's quickly touch upon Global objects which we can use without any objection and third-party imports. These objects/functions are nothing but built-in into Node.js objects. If you are already working on JavaScript you defiantly know about some of these objects like (console, setInterval, setTimeout, etc.).

The main difference between JavaScript (window) vs. Node.js global objects is, you can't get access to the screen-related function example: scroll, style, etc.

You can try following code examples in Node.js

// app.js setTimeout(() => {   console.log("5 seconds have passed"); }, 5000);  // Run this code again $ node app.js 
Enter fullscreen mode Exit fullscreen mode

Another useful global object is process, we will use this a lot in further lessons. process holds the latest state of your Node.js running process. It gives you access to read which environment your server is running or you can set custom environment variables while starting your server.

The common use case is to store credentials inside process to later use them in your program.

// Setting process variable $ export DB_NAME=demo_db $ export DB_PASSWORD=yes_you_are_right  // Run your app $ node app.js  // app.js const {DB_NAME, DB_PASSWORD} = process.env console.log(DB_NAME); // demo_db console.log(DB_PASSWORD); // yes_you_are_right 
Enter fullscreen mode Exit fullscreen mode

3. Modules + require()

So far (above), we wrote everything in a single file, which is good for practice programs and all. If you want to work on a real project where you might have other teams/people surrounding you and working on the same project, then recommended approach is to work with modules.

Modules are nothing but an approach to split your code into different-2 re-usable pieces.

It can be a file or a function within the same file, you must have heard of the philosophy of pure-functional programming where every function is responsible to do a single job.

Let's take an example, you want to count the length of an array.

// Create a new file next to your app.js // count.js  const count = (arr) => {   return arr.length } 
Enter fullscreen mode Exit fullscreen mode

Now you have count.js in place, it's time to make it re-usable for the application.

Before calling it a module, we need to make some changes to the count.js file.

// count.js  const count = (arr) => {   return arr.length }  module.exports = count;  // It will export this component as a module  
Enter fullscreen mode Exit fullscreen mode

Since we have the count module ready to include, let's call it in app.js.

// app.js  const count = require('./count');  // including `count` function as a module cosole.log(count([1, 98, 22, 41]))  // return: 4 
Enter fullscreen mode Exit fullscreen mode

You can send multiple modules from your single file.

// utils.js  const add = (a, b) => {   return a + b; }  const sub = (a, b) => {   return a - b; }  const getDatabaseName = () => process.env.DB_NAME;  module.exports = {   add,   sub,   getDatabaseName, };  // You also change your public function names module.exports = {   plus: add,   minus: sub,   DBName: getDatabaseName, }; 
Enter fullscreen mode Exit fullscreen mode

// app.js  const utils = require("./utils");  console.log(utils.add(1, 2));  // result: 3 console.log(utils.sub(2, 1));  // result: 1 console.log(utils.getDatabaseName());  // result: demo_db 
Enter fullscreen mode Exit fullscreen mode

4. Event emitting

Like the above-defined module, we have some built-in core Node.js modules available to use. One of the examples is the events module.

Similar to JavaScript click/onChange events Node.js has the capability to define your own events which can be used when needed.

// custom-events.js  const events = require('events'); const utils = require("./utils");  const eventEmitter = new events.EventEmitter();  eventEmitter.on("showSum", (a, b) => {   console.log(`Sum is: ${utils.add(a, b)}`); });  module.exports = eventEmitter; 
Enter fullscreen mode Exit fullscreen mode

Our event emitter module is now ready, let's use it in our application.

// app.js  const customEvents = require("./custom-events"); customEvents.emit("showSum", {a: 1, b: 2});  // Sum is: 3 
Enter fullscreen mode Exit fullscreen mode

5. Read/Write/Steam files

After learning event handling in Node.js let's do a quick check on file handling with Node.js core module fs.

Let's read the file first in order to try the fs module. But first, we need a file to read. Let's create a file first.

Read operation ()

// Inside the same directory $ touch read.txt $ vim ./read.text  // Add some content and save the file. 
Enter fullscreen mode Exit fullscreen mode

// app.js  const fs = require("fs");  const readMeFile = fs.readFileSync("./read.txt", "utf-8"); console.log(readMeFile);  // File contents 
Enter fullscreen mode Exit fullscreen mode

Write operation

// app.js  const fs = require("fs");  const readMeFile = fs.readFileSync("./read.txt", "utf-8"); fs.writeFileSync("./read-new.txt", readMeFile); 
Enter fullscreen mode Exit fullscreen mode

The above statement will simply read the contents from the read.txt file write another file read-new.txt file with its content.

There are two ways to read/write files, synchronous/asynchronous, above we are using sync. method to read the file. It prevents the program execute unless it reads the entire file.

On the other hand fs.readFile can read files asynchronously, sometime we use this method too.

Let's quick touch upon async. file read operation:

const fs = require("fs");  fs.readFile("./read.txt", "utf-8", (err, data) => {   if(err) {     console.error(err);   }   console.log(data); });  console.log("Am I first?");  // Result: // file content // Am I first? 
Enter fullscreen mode Exit fullscreen mode

In the example above you can see, text outside the readFile operation logged first. Async. operation is beneficial when your further operations are independent of the file read/write operations.

Hope you like the starting of the series, stay tuned for further posts in this series.

Happy reading!

Useful Links

Install Node.js

Global Objects

Events

File system

javascriptnodetutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.