Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions week1/hackyourtemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "hackyourtemperature",
"version": "1.0.0",
"type": "module",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^5.1.0",
"express-handlebars": "^8.0.3",
"node-fetch": "^3.3.2"
}
}
26 changes: 26 additions & 0 deletions week1/hackyourtemperature/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import express from 'express';
import { engine } from 'express-handlebars';

const app = express();
const PORT = 3000;

app.use(express.json());

app.engine('handlebars', engine());
app.set('view engine', 'handlebars');

app.get('/', (req, res) => {
res.send('hello from backend to frontend!');
});

app.post('/weather', (req, res) => {
const { cityName } = req.body;
if (!cityName) {
return res.status(400).send('Error: cityName is required in request body');
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice that you added validation of the payload 👍

res.send(`You submitted: ${cityName}`);
});

app.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's perfectly fine to hardcode the domain in an exercise like this, but in situations where the app may run in environments other than locally, that should be avoided.
One option is to avoid mentioning the host: console.log(`Server listening on port ${PORT}`);
Alternatively, you can read it from the HOST environment variable: const HOST = process.env.HOST || 'localhost';

});