Projects _ 100xDevs
Projects _ 100xDevs
Recap
Last week, we understood about HTTP Servers
2. Port
3. Methods
5. Status codes
6. Body
7. Routes
8. Express
3. Query params, Creating our own HTTP Server (Again). Does simple math
(sum server)
4. Middlewares
1. express.json / bodyParser.json
https://projects.100xdevs.com/pdf/http-deep-dive/HTTP-Deep-dive-1 1/17
12/27/24, 12:03 PM Projects | 100xDevs
2. cors
HTTP Deep dive 1 of 9
Headers
HTTP headers are key-value pairs sent between a client (like a web
browser) and a server in an HTTP request or response. They convey
metadata about the request or response, such as content type, auth
information etc.
Common headers
Request headers
The headers the client sends out in the request are known as request
headers
Response headers
The headers that the server responds with are known as the response
headers.
https://projects.100xdevs.com/pdf/http-deep-dive/HTTP-Deep-dive-1 2/17
12/27/24, 12:03 PM Projects | 100xDevs
Fetch API
There are 2 high level ways a browser can send requests to an HTTP server:
When you type a URL into the browser’s address bar and press
Enter, the browser sends an HTTP GET request to the server. This
request is used to retrieve resources like HTML pages, images, or
other content.
<!DOCTYPE html>
HTTP Deep dive 1 of 9
<html>
<body>
<div id="posts"></div>
<script>
async function fetchPosts() {
const res = await fetch("https://jsonplaceholder.typicode.com/posts/1");
const json = await res.json();
document.getElementById("posts").innerHTML = json.title;
}
fetchPosts();
</script>
</body>
</html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.7.6/axios.min.js"></
</head>
<body>
<div id="posts"></div>
<script>
async function fetchPosts() {
const res = await axios.get("https://jsonplaceholder.typicode.com/posts/1")
document.getElementById("posts").innerHTML = res.data.title;
}
fetchPosts();
</script>
</body>
</html>
https://projects.100xdevs.com/pdf/http-deep-dive/HTTP-Deep-dive-1 4/17
12/27/24, 12:03 PM Projects | 100xDevs
1. http://localhost:3000/multiply?a=1&b=2
2. http://localhost:3000/add?a=1&b=2
3. http://localhost:3000/divide?a=1&b=2
4. 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
Initialize node project
npm init -y
Install dependencies
touch index.js
https://projects.100xdevs.com/pdf/http-deep-dive/HTTP-Deep-dive-1 5/17
12/27/24, 12:03 PM Projects | 100xDevs
});
});
});
app.listen(3000);
res.json({
ans: a + b
})
});
res.json({
HTTP
ans:Deep
a / bdive 1 of 9
})
});
app.listen(3000);
res.json({
ans: a + b
})
});
https://projects.100xdevs.com/pdf/http-deep-dive/HTTP-Deep-dive-1 7/17
12/27/24, 12:03 PM Projects | 100xDevs
});
app.listen(3000);
Middlewares
In Express.js, middleware refers to functions that have access to the request
object ( req ), response object ( res ), and the next function in the
application's request-response cycle. Middleware functions can perform a
variety of tasks, such as
Try running this code and see if the logs comes or not
https://projects.100xdevs.com/pdf/http-deep-dive/HTTP-Deep-dive-1 8/17
12/27/24, 12:03 PM Projects | 100xDevs
res.json({
ans: a + b
})
});
res.json({
ans: a + b
})
});
https://projects.100xdevs.com/pdf/http-deep-dive/HTTP-Deep-dive-1 9/17
12/27/24, 12:03 PM Projects | 100xDevs
res.json({
ans: a + b
})
});
res.json({
ans: a + b
})
});
https://projects.100xdevs.com/pdf/http-deep-dive/HTTP-Deep-dive-1 10/17
12/27/24, 12:03 PM Projects | 100xDevs
// Middleware function
HTTP Deep
function dive 1 of 9
logRequest(req, res, next) {
console.log(`Request made to: ${req.url}`);
next();
}
res.json({
ans: a + b
})
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
Assignments on middleware
Try these out yourself.
https://projects.100xdevs.com/pdf/http-deep-dive/HTTP-Deep-dive-1 11/17
12/27/24, 12:03 PM Projects | 100xDevs
2. Create
HTTPaDeep
middleware
dive 1 of 9 that counts total number of requests sent to a
server. Also create an endpoint that exposes it
Commonly used
middlewares
https://projects.100xdevs.com/pdf/http-deep-dive/HTTP-Deep-dive-1 12/17
12/27/24, 12:03 PM Projects | 100xDevs
Through your journey of writing express servers , you’ll find some commonly
HTTP Deep dive 1 of 9
available (on npm) middlewares that you might want to use
1. express.json
The express.json() middleware is a built-in middleware function in Express.js
used to parse incoming request bodies that are formatted as JSON. This
middleware is essential for handling JSON payloads sent by clients in POST
or PUT requests.
// Send a response
res.send('Data received');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
https://projects.100xdevs.com/pdf/http-deep-dive/HTTP-Deep-dive-1 13/17
12/27/24, 12:03 PM Projects | 100xDevs
https://projects.100xdevs.com/pdf/http-deep-dive/HTTP-Deep-dive-1 14/17
12/27/24, 12:03 PM Projects | 100xDevs
app.listen(3000);
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.7.6/axios.min.js"></
</head>
<body>
<div id="posts"></div>
<script>
async function sendRequest() {
const res = await axios.get("http://localhost:3000/sum?a=1&b=2");
}
sendRequest();
</script>
</body>
</html>
cd public
npx serve
npm i cors
const
HTTPexpress = require("express");
Deep dive 1 of 9
const cors = require("cors");
const app = express();
app.use(cors());
res.json({
ans: a + b
})
});
app.listen(3000);
res.json({
ans: a + b
})
});
app.listen(3000);
https://projects.100xdevs.com/pdf/http-deep-dive/HTTP-Deep-dive-1 16/17
12/27/24, 12:03 PM Projects | 100xDevs
https://projects.100xdevs.com/pdf/http-deep-dive/HTTP-Deep-dive-1 17/17