Comprehensive Node.js Tutorials for Beginners to Advanced Developers

Master Node.js with our in-depth tutorials covering everything from the basics to advanced concepts. Learn how to build scalable web applications, work with APIs, manage databases, and more using Node.js. Perfect for both beginners and experienced developers looking to enhance their skills.



Building Web Applications with Express.js: A Step-by-Step Guide

This guide will walk you through creating a web application using Express.js, a popular framework for Node.js development. Express.js simplifies setting up a web server and handling user interactions through web pages.

Setting Up the Server

Importing Express.js

Start by importing Express.js using Node.js's require() function. This function returns an Express application object.

Syntax

  <!DOCTYPE html>
<html>
<head>
  <title>Submit Student Data</title>
</head>
<body>
  <form action="/submit-student-data" method="post">
    First Name: <input type="text" name="firstName" /><br />
    Last Name: <input type="text" name="lastName" /><br />
    <input type="submit" value="Submit" />
  </form>
</body>
</html>

Building Web Applications with Express.js: A Step-by-Step Guide

Setting Up the Server

Syntax

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

app.listen(5000, () => {
  console.log('Node server is running on port 5000!');
});
        
Output

Node server is running on port 5000!
        

Defining Routes

Syntax

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

app.get('/', (req, res) => {
  res.send('

Hello World!

'); }); app.post('/submit-data', (req, res) => { res.send('POST Request Received!'); }); app.put('/update-data', (req, res) => { res.send('PUT Request Received!'); }); app.delete('/delete-data', (req, res) => { res.send('DELETE Request Received!'); }); app.listen(5000, () => { console.log('Node server is running on port 5000!'); });
Output

Hello World!
POST Request Received!
PUT Request Received!
DELETE Request Received!
        

Explanation

Each route is defined with a method (GET, POST, PUT, DELETE) and a path (e.g., /, /submit-data). The callback function uses req (request object) and res (response object) to handle requests and send responses.

Running the Code

1. Save the updated code in app.js.
2. Run node app.js again.
3. Visit http://localhost:5000 to see "Hello World!".

Handling POST Requests with Forms

Create an HTML form to submit data to the server.

Syntax

<!DOCTYPE html>
<html>
<head>
  <title>Submit Student Data</title>
</head>
<body>
  <form action="/submit-student-data" method="post">
    First Name: <input type="text" name="firstName" /><br />
    Last Name: <input type="text" name="lastName" /><br />
    <input type="submit" value="Submit" />
  </form>
</body>
</html>
Output

[Form with fields for First Name and Last Name]
        

Body Parser for Handling POST Data

Syntax

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.urlencoded({ extended: false }));

app.get('/', (req, res) => {
  res.sendFile('index.html');
});

app.post('/submit-student-data', (req, res) => {
  const name = req.body.firstName + ' ' + req.body.lastName;
  res.send(name + ' Submitted Successfully!');
});

app.listen(5000, () => {
  console.log('Node server is running on port 5000!');
});
        
Output

[Name Submitted Successfully!]