Install [Link] and create a basic HTTP server using the built-in http module.
1. Install [Link]
On Windows or macOS:
1. Download [Link]: Go to the [Link] official website, and download the latest LTS version for
your operating system.
2. Install [Link]: Run the installer and follow the instructions. This will install both [Link] and
npm (Node's package manager).
3. You can verify the installation by running these commands:
node -v # Check [Link] version
npm -v # Check npm version
Once [Link] is installed, you can create a simple HTTP server using the built-in http module. Follow
these steps:
Step 1: Create a project folder
Create a new directory for your project and navigate into it.
mkdir my-node-server
cd my-node-server
Step 2: Create a [Link] file
In your project folder, create a file called [Link].
notepad [Link]
Step 3: Write the server code
Open [Link] with your preferred code editor and add the following code:
javascript
Copy code
// Load the http module to create an HTTP server.
const http = require('http');
// Define the hostname and port.
const hostname = '[Link]';
const port = 3000;
// Create an HTTP server and handle requests.
const server = [Link]((req, res) => {
// Set the response HTTP header to status 200 OK and content type text/html.
[Link] = 200;
[Link]('Content-Type', 'text/html');
// Send a simple HTML response.
[Link]('<h1>Hello, world! This is my first [Link] server!</h1>');
});
// Make the server listen on the specified port and hostname.
[Link](port, hostname, () => {
[Link](`Server running at [Link]
});
Step 4: Run the server
In your terminal, run the following command to start the server:
node [Link]
You should see output like:
Server running at [Link]
Step 5: Open the server in a browser
Now, open a web browser and go to [Link] You should see the following message
displayed in the browser:
Hello, world! This is my first [Link] server!
3. Stopping the server
To stop the server, press Ctrl + C in the terminal where the server is running.
Experiment with routing and handling different HTTP methods (GET,POST)
To experiment with routing and handling different HTTP methods (such as GET and POST), you'll need to
expand your basic HTTP server. This involves inspecting the request method ([Link]) and the
request URL ([Link]) to handle different routes and methods.
Below is an updated version of your [Link] file that handles different HTTP methods (GET, POST) and
different routes.
Steps to implement routing and handle different HTTP methods
1. Update [Link] to handle routing
const http = require('http');
// Define the hostname and port.
const hostname = '[Link]';
const port = 3000;
// Create an HTTP server to handle requests.
const server = [Link]((req, res) => {
// Set a default content-type for the response.
[Link]('Content-Type', 'text/html');
// Handle GET requests for the home page
if ([Link] === 'GET' && [Link] === '/') {
[Link] = 200;
[Link]('<h1>Welcome to the Home Page (GET)</h1>');
// Handle GET requests for the "/about" route
} else if ([Link] === 'GET' && [Link] === '/about') {
[Link] = 200;
[Link]('<h1>About Us (GET)</h1>');
// Handle POST requests for the "/submit" route
} else if ([Link] === 'POST' && [Link] === '/submit') {
let body = '';
// Collect the data sent with the POST request
[Link]('data', chunk => {
body += chunk;
});
// Once the request body has been fully received
[Link]('end', () => {
[Link] = 200;
[Link](`<h1>Form Submitted (POST)</h1><p>Received Data: ${body}</p>`);
});
// Handle other undefined routes
} else {
[Link] = 404;
[Link]('<h1>404 - Page Not Found</h1>');
}
});
// Start the server and listen on the defined port
[Link](port, hostname, () => {
[Link](`Server running at [Link]
});
Breakdown of the code:
1. Routing:
o We are manually checking the [Link] (the HTTP method) and [Link] (the route) to handle
different requests.
o We handle different routes like /, /about, and /submit.
o We respond differently based on the HTTP method (GET or POST).
2. GET requests:
o For GET requests to /, we respond with a welcome message.
o For GET requests to /about, we show an "About Us" message.
3. POST requests:
o For POST requests to /submit, we read the request body, collect the data (if any), and then
respond with the submitted data.
o We use [Link]('data', ...) to collect the data chunks and [Link]('end', ...) to finalize the response
after receiving all the data.
4. Fallback for 404:
o If the route doesn't match or the method is not handled, we return a 404 error with a message.
2. Run the server
Run the server as you did before:
node [Link]
You should see the following output:
Server running at [Link]
3. Test the server
Test GET requests:
• Home page (GET): Open a browser and visit [Link] You should see:
Welcome to the Home Page (GET)
• About page (GET): Visit [Link] You should see:
About Us (GET)
Test POST requests:
<html>
<body>
<form action="[Link] method="post">
<fieldset>
<legend>Login Form</legend>
<label>Enter UserName:<label><input type="text" name="userName"><br>
<label>Enter Password:</label><input type="password" name="password"><br>
<input type="submit"><input type="Reset">
</fieldset>
</form>
</body>
</html>