Week 9 Day 3
Week 9 Day 3
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
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.
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.
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.