Basic Node.js Routing Lab
In this lab, you will learn how to create and manage basic routing in a Node.js application. We will use the native http
module to create an HTTP server and handle different routes based on the requested path.
Steps
-
Your code contains two files
index.js
androutes.js
, arouter
function has already been declared for you inroutes.js
file, you have to write the mentioned logic in therouter
function. -
To write the logic of the function take a look at the stubs (comments in code) to help you understand the desired outcome. You have to send different text responsed based on the path the request was invoked from.
- If the
req.url
is equal to/hello
, your server should send the textHello, World!
with the status code200
- If the
req.url
is equal to/bye
, you server should send the textGoodbye, World!
with the status code200
- If the
req.url
does not match any of the above conditions it should send the textNot found
with the status code404
To send a valid Text via a HTTP server, you would have to additionally set the status code & Content-Type
header in the response. Here is a sample code
function router(req, res) { res.writeHead(404, { 'Content-Type': 'text/plain' }) res.end('Not found') }
In the above function, we're returning the response with the status code 404
and the text Not found
-
You have to import the
router
function in theindex.js
file and use this function to create a basic HTTP server. -
You can create a simple HTTP Server using
http.createServer(yourfunction)
method. Where you would replace theyourfunction
withrouter
in this scenario. -
The HTTP Server created should listen for requests on port
1337
. Use the.listen()
method on the created HTTP server.
You can verify if the HTTP server is working as expected by visiting the hello
and bye
route in the browser preview that is visible to you on the right side of your screen
Use ES modules for importing and exporting files. The
package.json
already has the property oftype
set tomodule