Welcome to the Node.js Basics Tutorial! If you're new to Node.js or looking to expand your knowledge, this guide will help you get started.

What is Node.js?

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It allows you to run JavaScript outside of a browser, on the server-side. This makes it a powerful tool for building scalable and efficient applications.

Features of Node.js

  • Asynchronous and Non-blocking: Node.js uses an event-driven, non-blocking I/O model which makes it lightweight and efficient.
  • Single-threaded: Node.js is single-threaded but uses an event loop to handle multiple operations concurrently.
  • Cross-platform: Node.js runs on multiple platforms including Windows, Linux, and macOS.

Install Node.js

To install Node.js, visit the official Node.js website. Download the appropriate version for your operating system and follow the installation instructions.

Hello World

Your first Node.js application is a simple "Hello World" program. Create a file called app.js and add the following code:

console.log('Hello World!');

Run the file using the Node.js command-line interface:

node app.js

You should see the output:

Hello World!

Modules

Node.js uses modules to organize your code. The built-in console module is an example of a module.

To use a module, you can import it into your file:

const console = require('console');

console.log('Hello World!');

Express.js

Express.js is a popular web application framework for Node.js. It simplifies the process of building web applications.

To get started with Express.js, install it using npm:

npm install express

Create a file called server.js and add the following code:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Run the server using the Node.js command-line interface:

node server.js

Open your web browser and go to http://localhost:3000. You should see the "Hello World!" message.

Next Steps

Happy coding! 🚀