Node.js is a powerful and versatile platform for building server-side applications using JavaScript. It has become increasingly popular among developers because of its ease of use, scalability, and robustness. In this article, we will provide a beginner’s guide on how to use Node.js.
1. Installing Node.js
Before you can use Node.js, you need to install it on your computer. You can download the installer from the official Node.js website. Once downloaded, run the installer and follow the instructions to install Node.js on your computer.
2. Setting up a project
After installing Node.js, you can create a new project by creating a new directory and navigating to it in the terminal. You can then initialize the project by running the following command:
“`
npm init
“`
This will create a package.json
file in your project directory that contains information about your project and its dependencies.
3. Installing dependencies
You can install Node.js packages and libraries using the Node Package Manager (npm). To install a package, run the following command:
npm install <package-name>
For example, to install the express
package, you can run:
npm install express
This will install the express
package and add it to your package.json
file.
4. Creating a server
To create a server in Node.js, you can use the http
module. Here is an example of how to create a simple server:
const http = require(‘http’);
const server = http.createServer((req, res) => {
res.writeHead(200, {‘Content-Type’: ‘text/plain’});
res.end(‘Hello, World!’);
});
server.listen(3000, () => {
console.log(‘Server running on port 3000’);
});
This code creates a server that listens on port 3000 and responds with the message “Hello, World!” to any HTTP request.
5. Routing
In a real-world application, you will likely want to define different routes for handling different types of requests. You can use the express
package to create a more sophisticated server with routing capabilities. Here is an example:
const express = require(‘express’);
const app = express();
app.get(‘/’, (req, res) => {
res.send(‘Hello, World!’);
});
app.get(‘/about’, (req, res) => {
res.send(‘About Us’);
});
app.listen(3000, () => {
console.log(‘Server running on port 3000’);
});
This code creates an express
app that defines two routes: the root route, which responds with “Hello, World!”, and the /about
route, which responds with “About Us”.
6. Database integration
Node.js can easily be integrated with various databases, such as MongoDB, MySQL, and PostgreSQL. You can use the appropriate package to connect to your database and perform operations on it. Here is an example using the mongodb
package:
const MongoClient = require(‘mongodb’).MongoClient;
const url = ‘mongodb://localhost:27017/myproject‘;
MongoClient.connect(url, (err, client) => {
console.log(‘Connected to database’);
const db = client.db(‘myproject’);
db.collection(‘users’).insertOne({
name: ‘John’,
age: 25
}, (err, result) => {
console.log(‘User inserted’);
client.close();
});
});
This code connects to a MongoDB database running on localhost
and inserts a new user document into the users
collection.
In conclusion, Node.js is a powerful platform for building server-side applications using JavaScript. With its ease of use, scalability, and robustness, it has become a developer’s favourite.