Skip to content
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
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
28 changes: 28 additions & 0 deletions assignments/hackyourtemperature/__tests__/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import app from '../server.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');
});
});
12 changes: 12 additions & 0 deletions assignments/hackyourtemperature/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
};
7 changes: 7 additions & 0 deletions assignments/hackyourtemperature/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
transform: {
'^.+\\.js$': 'babel-jest',
},
transformIgnorePatterns: ['node_modules/(?!node-fetch)'],
testEnvironment: 'node',
};
90 changes: 90 additions & 0 deletions assignments/hackyourtemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"name": "hackyourtemperature",
"version": "1.0.0",
"description": "",
"main": "server.js",
"dependencies": {
"accepts": "^1.3.8",
"array-flatten": "^1.1.1",
"body-parser": "^1.20.3",
"bytes": "^3.1.2",
"call-bind": "^1.0.7",
"content-disposition": "^0.5.4",
"content-type": "^1.0.5",
"cookie": "^0.6.0",
"cookie-signature": "^1.0.6",

Choose a reason for hiding this comment

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

Are you sure you committed the right package.json file? It looks like it has a lot of dependencies that aren't needed, and it's missing some that are needed in order to run the tests. Please check on this and update

"data-uri-to-buffer": "^4.0.1",
"debug": "^2.6.9",
"define-data-property": "^1.1.4",
"depd": "^2.0.0",
"destroy": "^1.2.0",
"ee-first": "^1.1.1",
"encodeurl": "^2.0.0",
"epxress": "^0.0.1-security",
"es-define-property": "^1.0.0",
"es-errors": "^1.3.0",
"escape-html": "^1.0.3",
"etag": "^1.8.1",
"express": "^4.21.0",
"express-handlers": "^1.0.0",
"fetch": "^1.1.0",
"fetch-blob": "^3.2.0",
"finalhandler": "^1.3.1",
"formdata-polyfill": "^4.0.10",
"forwarded": "^0.2.0",
"fresh": "^0.5.2",
"function-bind": "^1.1.2",
"get-intrinsic": "^1.2.4",
"gopd": "^1.0.1",
"has-property-descriptors": "^1.0.2",
"has-proto": "^1.0.3",
"has-symbols": "^1.0.3",
"hasown": "^2.0.2",
"http-errors": "^2.0.0",
"iconv-lite": "^0.4.24",
"inherits": "^2.0.4",
"ipaddr.js": "^1.9.1",
"media-typer": "^0.3.0",
"merge-descriptors": "^1.0.3",
"methods": "^1.1.2",
"mime": "^1.6.0",
"mime-db": "^1.52.0",
"mime-types": "^2.1.35",
"ms": "^2.0.0",
"negotiator": "^0.6.3",
"node-domexception": "^1.0.0",
"node-fetch": "^3.3.2",
"object-inspect": "^1.13.2",
"on-finished": "^2.4.1",
"parseurl": "^1.3.3",
"path-to-regexp": "^0.1.10",
"proxy-addr": "^2.0.7",
"qs": "^6.13.0",
"range-parser": "^1.2.1",
"raw-body": "^2.5.2",
"safe-buffer": "^5.2.1",
"safer-buffer": "^2.1.2",
"send": "^0.19.0",
"serve-static": "^1.16.2",
"set-function-length": "^1.2.2",
"setprototypeof": "^1.2.0",
"side-channel": "^1.0.6",
"statuses": "^2.0.1",
"toidentifier": "^1.0.1",
"type-is": "^1.6.18",
"undici-types": "^6.19.8",
"unpipe": "^1.0.0",
"utils-merge": "^1.0.1",
"vary": "^1.1.2",
"web-streams-polyfill": "^3.3.3"
},
"scripts": {
"test": "jest",
"start": "node server.js"

},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module"
}
37 changes: 37 additions & 0 deletions assignments/hackyourtemperature/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import fetch from 'node-fetch';
import { API_KEY } from './source/key.js';
import express from 'express';

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.cod === '404') {

Choose a reason for hiding this comment

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

This isn't blocking for the review, but something you can think about - what would happen if you get a different error code from the weather service? What would happen in your code and are you ok with that?

return res.status(404).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' });
}
});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});
1 change: 1 addition & 0 deletions assignments/hackyourtemperature/source/key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const API_KEY = '5b880f4ee3e43bab62bd1d03019f09c2';