forked from HackYourFuture/Node.js
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Beimnet w2 nodejs #26
Open
beimnet11
wants to merge
6
commits into
HackYourAssignment:main
Choose a base branch
from
beimnet11:beimnet-w2-nodejs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ba2908b
ex-1 node finished
beimnet11 a47edec
Revert "ex-1 node finished"
beimnet11 1c1315c
assignment node.js week2 done
beimnet11 9889a18
updated branch from local
beimnet11 a7834b8
updating but lots of dependencies
beimnet11 3b9d61e
updated, package.json is fixed and added app.js
beimnet11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import app from '../app.js'; | ||
import supertest from 'supertest'; | ||
|
||
const request = supertest(app); | ||
|
||
describe('POST /weather', () => { | ||
it('should return 400 if cityName is missing', async () => { | ||
const response = await request.post('/weather').send({}); | ||
expect(response.status).toBe(400); | ||
expect(response.body.error).toBe('cityName is required'); | ||
}); | ||
|
||
it('should return 404 if cityName is invalid', async () => { | ||
const response = await request | ||
.post('/weather') | ||
.send({ cityName: 'invalidCity' }); | ||
expect(response.status).toBe(404); | ||
expect(response.body.error).toBe('city not found'); | ||
}); | ||
|
||
it('should return temperature if cityName is valid', async () => { | ||
const response = await request | ||
.post('/weather') | ||
.send({ cityName: 'London' }); | ||
expect(response.status).toBe(200); | ||
expect(response.body.weatherText).toContain('The temperature in London is'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import fetch from 'node-fetch'; | ||
|
||
import express from 'express'; | ||
|
||
const API_KEY = '5b880f4ee3e43bab62bd1d03019f09c2'; | ||
|
||
const app = express(); | ||
app.use(express.json()); | ||
|
||
app.get('/', (req, res) => { | ||
res.send('hello from backend to frontend'); | ||
}); | ||
|
||
app.post('/weather', async (req, res) => { | ||
const cityName = req.body.cityName; | ||
const url = `http://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${API_KEY}&units=metric`; | ||
if (!cityName) { | ||
return res.status(400).json({ error: 'cityName is required' }); | ||
} | ||
try { | ||
const response = await fetch(url); | ||
const data = await response.json(); | ||
|
||
if (data.status === '404') { | ||
return res.status(404).json({ weatherText: 'city is not found' }); | ||
} else if (!response.ok) { | ||
return res.status(response.status).json({ error: 'city not found' }); | ||
} | ||
|
||
const temperature = data.main.temp; | ||
|
||
res.status(200).json({ | ||
weatherText: `The temperature in ${cityName} is ${temperature}°C`, | ||
}); | ||
} catch (error) { | ||
res.status(500).json({ error: 'server error' }); | ||
} | ||
}); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = { | ||
presets: [ | ||
[ | ||
'@babel/preset-env', | ||
{ | ||
targets: { | ||
node: 'current', | ||
}, | ||
}, | ||
], | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
transform: { | ||
'^.+\\.js$': 'babel-jest', | ||
}, | ||
transformIgnorePatterns: [], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "hackyourtemperature", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "server.js", | ||
"scripts": { | ||
"test": "jest", | ||
"start": "node server.js" | ||
}, | ||
"type": "module", | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"express": "^4.21.1", | ||
"express-handlers": "^1.0.0", | ||
"node-fetch": "^3.3.2", | ||
"nodemon": "^3.1.7" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.25.8", | ||
"@babel/preset-env": "^7.25.8", | ||
"@babel/register": "^7.25.7", | ||
"babel-jest": "^29.7.0", | ||
"jest": "^29.7.0", | ||
"supertest": "^7.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import app from './app.js'; | ||
|
||
const port = 3000; | ||
app.listen(port, () => { | ||
console.log(`Server is running on http://localhost:${port}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const AIP_KEY = '5b880f4ee3e43bab62bd1d03019f09c2'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"devDependencies": { | ||
"jest": "^29.7.0", | ||
"supertest": "^7.0.0" | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Looks like this is being used again (it's also defined in key.js). It's always preferable to keep security keys separate from the actual code as they are sensitive information. In fact, in a production setting, you would not even check the api key into github as that could expose it as well.
I won't block on this since the code is working well and you've move on the the next module, but something to keep in mind!