Skip to content

Hossein-w2-node.js #14

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ node_modules
npm-debug.log
package-lock.json
yarn-error.log
.env
*.bkp

week3/prep-exercise/server-demo/
File renamed without changes.
29 changes: 29 additions & 0 deletions assignments/hackyourtemperature/__tests__/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { app } from '../app.js';
import supertest from 'supertest';

const request = supertest(app);

describe('POST /weather', () => {
it('should return 400 if no cityName is provided', async () => {
const response = await request.post('/weather').send({});

expect(response.status).toBe(400);
expect(response.body).toEqual({
weatherText: 'City name is required!'
});
});

it('should return 404 if cityName is gibberish', async () => {
const response = await request.post('/weather').send({ cityName: 'aslkdjflaskjd' });

expect(response.status).toBe(404);
expect(response.body).toEqual({ weatherText: 'City is not found!' });
});

it('should return 200 with correct cityName', async () => {
const response = await request.post('/weather').send({ cityName: 'Amsterdam' });

expect(response.status).toBe(200);
expect(response.body.weatherText).toContain('Amsterdam');
});
});
40 changes: 40 additions & 0 deletions assignments/hackyourtemperature/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import express from 'express';
import keys from './sources/keys.js';
import fetch from 'node-fetch';

const app = express();

app.use(express.json());

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

app.post('/weather', async (req, res) => {
try {
const { cityName } = req.body;

if (!cityName) {
return res.status(400).json({ weatherText: 'City name is required!' });
}

const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${keys.API_KEY}`
);

if (!response.ok) {
return res.status(404).json({ weatherText: 'City is not found!' });
}

const data = await response.json();
const temperatureInCelsius = Math.floor(data.main.temp - 273.15);

res.status(200).json({
weatherText: `${cityName}: ${temperatureInCelsius}°C`
});
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});

export { app };
13 changes: 13 additions & 0 deletions assignments/hackyourtemperature/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
presets: [
[
// This is a configuration, here we are telling babel what configuration to use
'@babel/preset-env',
{
targets: {
node: 'current'
}
}
]
]
};
8 changes: 8 additions & 0 deletions assignments/hackyourtemperature/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
// Tells jest that any file that has 2 .'s in it and ends with either js or jsx should be run through the babel-jest transformer
transform: {
'^.+\\.jsx?$': 'babel-jest'
},
// By default our `node_modules` folder is ignored by jest, this tells jest to transform those as well
transformIgnorePatterns: []
};
27 changes: 27 additions & 0 deletions assignments/hackyourtemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "hackyourtemperature",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"test": "jest",
"start": "node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"dotenv": "^16.4.7",
"express": "^4.21.1",
"express-handlebars": "^8.0.1",
"node-fetch": "^3.3.2"
},
"type": "module",
"devDependencies": {
"@babel/core": "^7.26.0",
"@babel/preset-env": "^7.26.0",
"babel-jest": "^29.7.0",
"jest": "^29.7.0",
"supertest": "^7.0.0"
}
}
7 changes: 7 additions & 0 deletions assignments/hackyourtemperature/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { app } from './app.js';

const port = 3000;

app.listen(port, () => {
console.log(`App listening on port ${port}`);
});
8 changes: 8 additions & 0 deletions assignments/hackyourtemperature/sources/keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import dotenv from 'dotenv';
dotenv.config();

const keys = {
API_KEY: process.env.API_KEY,
};

export default keys;
File renamed without changes.