-
Notifications
You must be signed in to change notification settings - Fork 10
DARYNA_TKACHENKO-w1-Node.js #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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" | ||
| } | ||
| } |
| 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'); | ||
| } | ||
| res.send(`You submitted: ${cityName}`); | ||
| }); | ||
|
|
||
| app.listen(PORT, () => { | ||
| console.log(`Server listening on http://localhost:${PORT}`); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| }); | ||
There was a problem hiding this comment.
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 👍