Skip to content

NwoluDavid/learningNode.js

Repository files navigation

This README.md include topics on Node.js built-in modules, the Path module, the HTTP module, installing dependencies using npm, and dependency management.

# Node.js Backend Development Guide

Welcome to the Node.js backend development guide! This document will help you get started with Node.js by covering various essential topics.

## Table of Contents
1. [Installation of Node.js and Setup](#installation-of-nodejs-and-setup)
2. [Node.js Global Variables](#nodejs-global-variables)
3. [Node.js Built-in Modules](#nodejs-built-in-modules)
4. [Path Module](#path-module)
5. [HTTP Module](#http-module)
6. [Installing Dependencies Using npm](#installing-dependencies-using-npm)
7. [Dependency Management Using npm](#dependency-management-using-npm)

## Installation of Node.js and Setup

### 1. Download and Install Node.js

To get started with Node.js, you need to download and install it on your system. Node.js comes with npm (Node Package Manager), which is essential for managing your project's dependencies.

1. **Visit the Official Node.js Website**: Go to the [Node.js official website](https://nodejs.org/).
2. **Download the Installer**: Download the installer suitable for your operating system (Windows, macOS, or Linux).
3. **Run the Installer**: Follow the prompts in the installer to complete the installation process.

### 2. Verify the Installation

After installing Node.js, you can verify the installation by opening your terminal or command prompt and running the following commands:

```sh
node -v
npm -v

These commands will display the installed versions of Node.js and npm, respectively.

3. Initialize a Node.js Project

To create a new Node.js project, follow these steps:

  1. Create a Project Directory:

    mkdir my-node-project
    cd my-node-project
  2. Initialize the Project:

    npm init -y

    This command will create a package.json file with default settings.

4. Install Dependencies

You can install project dependencies using npm. For example, to install Express (a popular Node.js web application framework), run:

npm install express

Node.js Global Variables

Node.js provides several global variables that are available in all modules. These variables are not necessary to require in your script.

1. __dirname

__dirname is a string representing the directory name of the current module. This is useful for constructing paths relative to the current file.

Example:

console.log(__dirname);
// Output: The directory path of the current module

2. __filename

__filename is a string representing the filename of the current module. This includes the full path of the file.

Example:

console.log(__filename);
// Output: The full path of the current module

3. require()

require() is a function to import modules, JSON, and local files. It is used to include modules that exist in separate files.

Example:

const fs = require('fs');
// Now you can use the 'fs' module

4. module

module is an object representing the current module. module.exports is used to export objects and functions from a module.

Example:

module.exports = {
  myFunction: function() {
    console.log('This is my function');
  }
};

5. exports

exports is a shorthand to module.exports. It provides a way to export module components.

Example:

exports.myFunction = function() {
  console.log('This is my function');
};

6. process

process is an object that provides information and control over the current Node.js process. It includes properties like process.env for environment variables and methods for interacting with the process runtime.

Example:

console.log(process.env);
// Output: An object containing the user environment

Node.js Built-in Modules

Node.js comes with a set of built-in modules that provide various functionalities to work with files, HTTP, paths, etc., without needing to install additional packages.

Working with Built-in Modules

To use a built-in module, you need to require it in your script. Here's how you can use the fs (File System) module:

const fs = require('fs');

// Read a file asynchronously
fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log(data);
});

Path Module

The Path module provides utilities for working with file and directory paths.

Using the Path Module

To use the Path module, require it in your script:

const path = require('path');

// Join paths
const joinedPath = path.join(__dirname, 'example.txt');
console.log(joinedPath);

// Get the directory name of a path
const dirName = path.dirname(joinedPath);
console.log(dirName);

// Get the base name of a path
const baseName = path.basename(joinedPath);
console.log(baseName);

// Get the extension of a path
const extName = path.extname(joinedPath);
console.log(extName);

HTTP Module

The HTTP module allows you to create an HTTP server in Node.js.

Creating a Simple HTTP Server

Here’s how to create a simple HTTP server that responds with "Hello, World!":

const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!\n');
});

const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}/`);
});

Installing Dependencies Using npm

npm (Node Package Manager) is used to install, update, and manage dependencies in your Node.js project.

Installing a Dependency

To install a dependency, use the npm install command followed by the package name:

npm install express

This will install the Express package and add it to the dependencies section of your package.json file.

Installing a Development Dependency

To install a package as a development dependency (only needed during development), use the --save-dev flag:

npm install --save-dev nodemon

Dependency Management Using npm

Managing dependencies effectively is crucial for maintaining a stable project.

Viewing Installed Dependencies

To see all installed dependencies in your project, you can check the package.json file or run:

npm list

Updating Dependencies

To update a specific dependency to the latest version, use:

npm update <package-name>

To update all dependencies to their latest versions as specified by the version ranges in package.json:

npm update

Removing Dependencies

To remove a dependency from your project, use the npm uninstall command:

npm uninstall <package-name>

This will remove the package from node_modules and update package.json to reflect the removal.

Managing Versions

You can specify version ranges for your dependencies in package.json:

  • "express": "^4.17.1": Compatible with version 4.17.1 and higher, but not 5.0.0.
  • "express": "~4.17.1": Compatible with version 4.17.x, but not higher than 4.17.
  • "express": "4.17.1": Exactly version 4.17.1.

Conclusion

This guide covers the basics of setting up Node.js, introduces key global variables, and explains how to work with built-in modules, the Path module, the HTTP module, and npm for dependency management. As you continue to learn and build with Node.js, these concepts and tools will be fundamental to your development workflow.

Happy coding!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published