node.js: URL-based requests

Hi people!!

Today I will demonstrate how node.js can respond based on the URL.

1st step: Open the command line and enter the following commands:
- mkdir URL-requests
- npm init
- touch require.js

Before proceeding to the 2nd step, the theme for our code will be about a computer store and that we will have the following categories:
- Laptops;
- Smartphones;
- Tablets.

2nd step: Insert the following code

var http = require('http');var server = http.createServer(function(req, res) {var cat = req.url;if (cat == "/laptops"){res.end('Laptops Category');} else if (cat == "/smartphones") {res.end('Smartphones Category');} else if (cat == "/tablets") {res.end('Tablets Category');} else {res.end("Technology Store")}}).listen(8085);

3rd step: Run the following command

node require.js

And ready, these are the final results:

http://localhost:8085/
http://localhost:8085/laptops
http://localhost:8085/smartphones
http://localhost:8085/tablets

I hope you enjoyed!!

--

--