In this blog on Getting Started With NodeJS, I will explain how to start working with NodeJs using simple examples.
Node.js is a popular runtime for server-side JavaScript applications. It’s built on the V8 JavaScript runtime and allows you to run JavaScript code on the server side, enabling the development of scalable and efficient web applications. Here’s a step-by-step guide to getting started with Node.js:
Step 1: Install Node.js
Before you start using Node.js, you need to install it on your machine. You can download the latest version from the official website: Node.js Downloads (https://nodejs.org/en).
Installing Node.js
![Download and Install Node.js](https://www.programmingempire.com/wp-content/uploads/2024/02/image.png)
![Accept the License Agreement](https://www.programmingempire.com/wp-content/uploads/2024/02/image-1.png)
![Select the Destination Folder](https://www.programmingempire.com/wp-content/uploads/2024/02/image-2.png)
![Select the Features to be installed](https://www.programmingempire.com/wp-content/uploads/2024/02/image-3.png)
![Click on Install Button](https://www.programmingempire.com/wp-content/uploads/2024/02/image-4.png)
![Node.js Setup](https://www.programmingempire.com/wp-content/uploads/2024/02/image-5.png)
Step 2: Verify Installation
After installing Node.js, open a terminal or command prompt and run the following commands to verify that Node.js and npm (Node Package Manager) are installed:
node -v
npm –v
![Verify the Node.js Installation](https://www.programmingempire.com/wp-content/uploads/2024/02/image-6.png)
Step 3: Create a Simple Node.js Application
- Create a new folder for your project.
- Create a new file named app.js using a text editor of your choice (e.g., VSCode, Sublime Text, Atom):
// app.js
console.log('Hello, Node.js!');
Save the file and run it using the following command:
node app.js
![Output of app.js](https://www.programmingempire.com/wp-content/uploads/2024/02/image-7.png)
Step 4: NPM (Node Package Manager)
NPM is the package manager for Node.js. It allows you to install and manage packages (libraries) for your Node.js projects.
Initialize a new package.json file for your project. Run the following command and follow the prompts:
npm init
Install a package. Let’s install the lodash library as an example:
npm install lodash
![Installing a Package Using NPM](https://www.programmingempire.com/wp-content/uploads/2024/02/image-8.png)
This command installs lodash and adds it to your package.json file as a dependency.
Step 5: Create a Web Server
Now, let’s create a simple web server using the Express framework. Install Express using npm:
Create a new file server.js:
// server.js
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello, Express!');
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
Run the server:
node server.js
![Run the Server](https://www.programmingempire.com/wp-content/uploads/2024/02/image-9.png)
![Node.js app running](https://www.programmingempire.com/wp-content/uploads/2024/02/image-10.png)
Further Reading
Understanding NodeJS With Examples
- Angular
- ASP.NET
- Bootstrap
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- NodeJS
- PHP
- Power Bi
- Python
- Scratch 3.0
- SQL
- TypeScript
- VB.NET
- Web Designing
- XML
![programmingempire](https://www.programmingempire.com/wp-content/uploads/2021/10/pefavicon.png)