Comprehensive Tutorial for Node.js
Introduction to Node.js
Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside of a web browser. It is designed to build scalable network applications, and it uses an event-driven, non-blocking I/O model.
Installation
To start using Node.js, you need to install it on your machine. Visit the official Node.js website and download the installer for your operating system.
After downloading, run the installer and follow the setup instructions. Once installed, you can verify the installation by running the following commands in your terminal:
node -v
npm -v
You should see the versions of Node.js and npm (Node Package Manager) printed in the terminal.
Creating a Simple Node.js Application
Let's create a simple Node.js application that prints "Hello, World!" to the console.
First, create a new directory for your project and navigate into it:
mkdir my-node-app
cd my-node-app
Next, create a new file named app.js
and open it in your favorite code editor. Add the following code to app.js
:
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 = 3000; server.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}/`); });
To run the application, navigate to the project directory in your terminal and execute:
node app.js
Open your web browser and go to http://localhost:3000/
. You should see "Hello, World!" displayed on the page.
Working with Modules
Node.js has a built-in module system. Modules are JavaScript files that export functions, objects, or values that can be imported and used in other files.
Let's create a new module and use it in our application. Create a new file named greet.js
in the project directory and add the following code:
function greet(name) { return `Hello, ${name}!`; } module.exports = greet;
Now, update app.js
to use the greet
module:
const http = require('http'); const greet = require('./greet'); const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end(greet('World') + '\n'); }); const PORT = 3000; server.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}/`); });
Run the application again using node app.js
and refresh the browser. You should see "Hello, World!" displayed on the page, but this time it's generated using the greet
module.
Using npm Packages
npm (Node Package Manager) is a package manager for Node.js. It allows you to install and manage third-party libraries and tools. Let's install and use a popular npm package called lodash
.
First, initialize a new npm project by running the following command in the project directory:
npm init -y
This will create a package.json
file with default settings. Next, install the lodash
package:
npm install lodash
Update app.js
to use lodash
:
const http = require('http'); const _ = require('lodash'); const greet = require('./greet'); const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); const message = _.capitalize(greet('world')); res.end(message + '\n'); }); const PORT = 3000; server.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}/`); });
Run the application again using node app.js
and refresh the browser. You should see "Hello, world!" displayed on the page, with "Hello" capitalized by lodash
.
Conclusion
In this tutorial, we covered the basics of Node.js, including installation, creating a simple application, working with modules, and using npm packages. Node.js is a powerful tool for building scalable network applications, and with its vast ecosystem of packages, you can extend its functionality to suit your needs.