Skip to content
Snippets Groups Projects
Commit cc7b2cfb authored by Oshani Weerakoon's avatar Oshani Weerakoon
Browse files

Exercise 9 + Instructions

parent 866406b4
No related branches found
No related tags found
No related merge requests found
Here's how you can set up a simple server and then inspect and minimize the HTTP headers manually.
**Backend Setup:**
1. **Install Node.js**: If you haven't already, install Node.js from [nodejs.org](https://nodejs.org/).
2. **Set up your project directory and install Express**:
```bash
mkdir my_project
cd my_project
npm init -y
npm install express
```
3. **Create a file named `server.js`** and add the following code to set up an Express server that serves a static file with minimal headers:
```javascript
const express = require('express');
const app = express();
const port = 3000;
app.use((req, res, next) => {
// Express adds a header "X-Powered-By: Express" by default, but it's not needed
res.removeHeader("X-Powered-By");
next();
});
app.get('/', (req, res) => {
// Serve a simple text file or HTML with minimal headers
res.type('text/plain');
res.send('Hello, World!');
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
```
**Testing the Setup:**
1. **Run the server**:
```bash
node server.js
```
2. **Manually inspect headers** using browser developer tools:
- Open your web browser and navigate to `http://localhost:3000`.
- Open the browser's developer tools (usually F12 or right-click > Inspect).
- Go to the Network tab and refresh the page.
- Click on the request to your server and inspect the headers.
You should see minimal headers being used. If you find unnecessary headers, you can configure your server to remove them by modifying the middleware in your `server.js` file.
For example, if you find that your server is sending a `Server` header and you want to remove it, you could add the following line inside your middleware function:
```javascript
res.removeHeader("Server");
```
**Note**: The impact of removing HTTP headers on energy consumption is generally very small, especially for small-scale applications. However, for large-scale applications with millions of requests, even small optimizations can lead to significant savings in bandwidth and energy use. It's also a good practice for security, as it can reduce the amount of information potentially useful to attackers.
\ No newline at end of file
const express = require('express');
const app = express();
const port = 3000;
app.use((req, res, next) => {
// Express adds a header "X-Powered-By: Express" by default, but it's not needed
res.removeHeader("X-Powered-By");
res.removeHeader("Remote Address");
next();
});
app.get('/', (req, res) => {
// Serve a simple text file or HTML with minimal headers
res.type('text/plain');
res.send('Hello, World!');
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment