Create an HTTP Server
Course: HTTP Deep dive - chapter 4
It should have 4 routes
- http://localhost:3000/multiply?a=1&b=2
- http://localhost:3000/add?a=1&b=2
- http://localhost:3000/divide?a=1&b=2
- http://localhost:3000/subtract?a=1&b=2
Inputs given at the end after
?are known as query parameters (usually used in GET requests)
The way to get them in an HTTP route is by extracting them from the req argument (req.query.a , req.query.b)
Steps to follow
Section titled “Steps to follow”- Initialize node project
npm init -y- Install dependencies
npm install express- Create an empty index.js
touch index.js- Write the code to create 4 endpoints
const express = require("express");
const app = express();
app.get("/sum", function(req, res) {
});
app.get("/multiply", function(req, res) {
});
app.get("/divide", function(req, res) {
});
app.get("/subtract", function(req, res) {
});
app.listen(3000);- Fill in the handler body
const express = require("express");
const app = express();
app.get("/sum", function(req, res) { const a = req.query.a; const b = req.query.b;
res.json({ ans: a + b })});
app.get("/multiply", function(req, res) { const a = req.query.a; const b = req.query.b; res.json({ ans: a * b })});
app.get("/divide", function(req, res) { const a = req.query.a; const b = req.query.b; res.json({ ans: a / b })
});
app.get("/subtract", function(req, res) { const a = req.query.a; const b = req.query.b; res.json({ ans: a - b })});
app.listen(3000);- Test it in the browser

💡
What do you think is wrong here?
- Correct the solution
const express = require("express");
const app = express();
app.get("/sum", function(req, res) { const a = parseInt(req.query.a); const b = parseInt(req.query.b);
res.json({ ans: a + b })});
app.get("/multiply", function(req, res) { const a = req.query.a; const b = req.query.b; res.json({ ans: a * b })});
app.get("/divide", function(req, res) { const a = req.query.a; const b = req.query.b; res.json({ ans: a / b })
});
app.get("/subtract", function(req, res) { const a = parseInt(req.query.a); const b = parseInt(req.query.b); res.json({ ans: a - b })});
app.listen(3000);