0% found this document useful (0 votes)
7 views10 pages

Week 9 Day 3

Fcygichbhvivivhyctc Xtxycy

Uploaded by

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

Week 9 Day 3

Fcygichbhvivivhyctc Xtxycy

Uploaded by

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

Building Web Servers with

Node.js
Welcome to this presentation on building web servers with Node.js! This
guide will walk you through the fundamentals of creating web servers using
the powerful http module and then explore the benefits of using a popular
framework, Express.js. You'll learn how to handle requests, send responses,
implement routing, and work with middleware. By the end, you'll have a solid
understanding of building functional web applications in Node.js.
Node.js: The Core of Web Server Creation
The http Module Creating a Server
The http module is fundamental to Node.js web server The http.createServer() method takes a callback function that
development. It provides the tools to create a server that listens handles incoming requests. This function receives request (req)
for incoming requests and sends back responses. This forms and response (res) objects. The res object is used to send
the basis of creating web applications. responses to the client.
Handling Requests and Sending
Responses

1 Request Object (req) 2 Response Object (res)


The req object holds The res object is used to send
information about the responses to the client. Use
incoming request, such as the res.write() to send data in
URL path, method, headers, chunks and res.end() to
and body. You can access finalize the response. You can
these properties to also set headers and status
understand the client's codes with res.setHeader()
request. and res.statusCode,
respectively.
Basic Routing in Node.js
Handling Different Paths Using Conditional Statements
Routing allows your server to handle requests for different URLs To implement routing, you can use conditional statements to
differently. You can respond with different content based on the check the req.url property, which represents the requested URL
requested path. path. This allows you to execute specific logic for each route.
Dynamic Responses with Query
Strings

What are Query Strings? Extracting Query Parameters


Query strings are additional The url module in Node.js helps you
parameters appended to a URL, parse URLs and extract query string
typically after a question mark (?). parameters. You can access these
They provide extra information to the parameters for dynamic content or
server. filtering based on user input.
Serving HTML Content
Content Type Using res.write()
When sending HTML content, it's You can use the res.write()
important to set the Content- method to send HTML strings as
Type header to 'text/html' to a response. You can either
inform the browser that the embed HTML directly in your
response is HTML. This ensures code or build it dynamically using
correct rendering. JavaScript logic.
Serving Static Content
1 File System Access
The fs (file system) module lets you read files from the
filesystem. You can use fs.readFileSync() to read the entire
contents of a file into a string.

2 Serving HTML Files


After reading a static HTML file, you can send its content as a
response to the client. This allows you to separate your server
logic from your HTML files, making maintenance easier.
Working with JSON Data
JSON as Data Exchange Format
JSON is a widely used format for exchanging data between
1
web servers and clients. Its simple structure makes it easy to
parse and use.

JSON.stringify()
You can convert JavaScript objects to JSON strings using
2
JSON.stringify(). This enables sending structured data as
responses. It's common to use JSON for API endpoints.

Setting Content Type


When sending JSON data, set the Content-Type header to
3
'application/json' to indicate to the client that the response is in
JSON format.
Introducing Middleware
Middleware Functions
Middleware functions are functions that execute between receiving a request and
1 sending a response. They can modify the request object, handle errors, or perform
other tasks.

Example: Logging Middleware


Middleware can be used to log requests for debugging or tracking user
2
activity. In this example, a middleware function logs the request method
and URL.

Global vs Route-Specific
3 Middleware can be applied globally to all routes or specifically
to certain routes, allowing you to control its behavior.
Express.js: A Powerful Web Framework
Simplified Server Creation
1 Express.js provides a concise and easy-to-use API for creating web servers. It simplifies common tasks and
reduces the amount of boilerplate code required.

Routing with Express


Express makes routing very intuitive. You can define routes using app.get(),
2
app.post(), app.put(), and app.delete() for different HTTP methods. These routes can
handle dynamic parameters.

Middleware in Express
Express supports middleware, which allows you to create
3 reusable functions that modify requests, handle errors, or
perform other tasks. You can apply middleware globally or to
specific routes.

You might also like