0% found this document useful (0 votes)
2 views

Creating Web Server in Node.js With HTTP Module

The document provides a guide on creating a basic HTTP server using Node.js's http module, detailing how to handle requests and send responses. It covers returning different content types, serving HTML pages from files, and managing routes based on URL and HTTP methods. An activity is included to practice setting up an HTTP server with specific routes and responses.

Uploaded by

jethrozarate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Creating Web Server in Node.js With HTTP Module

The document provides a guide on creating a basic HTTP server using Node.js's http module, detailing how to handle requests and send responses. It covers returning different content types, serving HTML pages from files, and managing routes based on URL and HTTP methods. An activity is included to practice setting up an HTTP server with specific routes and responses.

Uploaded by

jethrozarate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

CREATING A WEB

SERVER IN NODE.JS
WITH HTTP MODULE
CREATING A BASIC HTTP
SERVER
Learning back-end development is understanding how to set up a
server that can handle requests and send responses. Node.js’s http
module provides the ability to create such a server easily.
Import the http module, then use http.createServer() to set up a basic
server.
The server uses a callback function to handle each request, where req
represents the incoming request, and res represents the outgoing
response.
EXAMPLE
Here’s a simple HTTP server that listens on port 5000 and sends a
“Hello, World!” response to every request.
RETURNING DIFFERENT
TYPES OF CONTENT
Understand how to respond with different types of content (plain text,
JSON, HTML) and how to set the appropriate Content-Type headers for
each. This is a key skill in building dynamic and interactive web
applications.

Use res.writeHead() to set the Content-Type header based on the content


you’re returning.
Serving different content types helps cater to varied use cases, such as
plain text for logs, JSON for APIs, and HTML for web pages.
EXAMPLE
Here’s an extended
server that returns
different content based
on the requested URL.
SERVING AN HTML PAGE
FROM A FILE
HTML pages are usually served from files rather than being
embedded directly in the server code. This section teaches how to
use the fs module to read an HTML file and serve it as a response.

Use fs.readFile() to load the HTML file content.


Send the content as the response, along with the Content-Type:
text/html header.
EXAMPLE
This example serves an
HTML page from a file called
index.html.
MANAGING ROUTES USING
AN HTTP REQUEST OBJECT
Routing allows a server to handle different requests based on the
URL and HTTP method. This is an essential skill for building more
complex applications that handle multiple types of requests.

Use req.url to determine the path and req.method to determine


the type of request (GET, POST, etc.).
EXAMPLE
The server
responds
differently to GET
and POST
requests.
ACTIVITY 1
Create an HTTP server that listens on port 3000 and sets up
routes for handling different requests. Using Postman
Set Up Routes:
•Define the following routes:
•GET /: Respond with "Welcome to the Home Page".
•GET /json: Respond with JSON data, { "message": "Hello, JSON
world!" }.
•GET /html: Serve the index.html file with <h1>Your Basic
Information</h1>.
•POST /submit: Accept a JSON payload and respond with a confirmation
message.

You might also like