Skip to content
Snippets Groups Projects
Commit 86db390b authored by benjamin.sitbon's avatar benjamin.sitbon
Browse files

backend

parent 4e350a17
Branches
No related tags found
No related merge requests found
{
"rootApi": "/api/v1/",
"port": 8081
}
exports.success = function(result){
return {
status: 'success',
result: result
}
}
exports.error = function(message){
return {
status: 'error',
result: message
}
}
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error 404</title>
</head>
<body>
<div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%);">
<img src="https://cdn2.scratch.mit.edu/get_image/gallery/3004757_200x130.png?v=1472688587.05" alt="none">
<p style="text-align: center; font-size: x-large;">Error 404 Not found !</p>
</div>
</body>
</html>
\ No newline at end of file
This diff is collapsed.
...@@ -4,17 +4,21 @@ ...@@ -4,17 +4,21 @@
"description": "Server for Check Norris' jokes", "description": "Server for Check Norris' jokes",
"main": "server.js", "main": "server.js",
"dependencies": { "dependencies": {
"express": "^4.17.1" "body-parser": "^1.19.0",
"ejs": "^3.1.6",
"express": "^4.17.1",
"morgan": "^1.10.0",
"node-fetch": "^2.6.1"
}, },
"devDependencies": {}, "devDependencies": {},
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js" "start": "nodemon server.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://gitedu.hesge.ch/jeremy.gobet/app-et-archi-web-tp-2020" "url": "https://gitedu.hesge.ch/sitbon.benjamin/app-et-archi-web-tp-2020"
}, },
"author": "Jeremy Gobet", "author": "Sitbon Benjamin - Beck Léonard",
"license": "ISC" "license": "ISC"
} }
const {success,error} = require("./functions");
const bodyParser = require('body-parser');
const express = require('express'); const express = require('express');
const app = express();
const config = require('./config');
const fetch = require("node-fetch");
function Pokemons(){
return fetch("https://pokeapi.co/api/v2/pokemon/?limit=900")
.then(response => response.json())
.catch(error => console.error(error));
}
//effectue une requête
function get(that) {
return fetch(that)
.then(response => response.json())
.catch(error => console.error(error));
}
const pokemons = [
]
const cart = [
]
let api = Pokemons();
api.then(pok =>{
for(let i = 0; i<807; i++){
let type1 = "none";
let type2 = "none";
get(pok.results[i].url).then(stat => {
if(stat.types.length > 1){
type1 = stat.types[0].type.name;
type2 = stat.types[1].type.name;
}
else{
type1 = stat.types[0].type.name;
}
let pokemon = {
id: i+1,
name: pok.results[i].name,
type: {
0:type1,
1:type2 }
}
pokemons.push(pokemon);
})
}
})
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true}));
app.use(express.static('../frontend'));
/*app.get('*',function(err,res,next){
res.status(404).sendFile(__dirname +'/html/error404.html');
})*/
app.get(config.rootApi + "pokemons/:id",function (req,res){
let index = getIndex(req.params.id);
(index < 0) ? res.sendFile(__dirname +'/html/error404.html') : res.json(success(pokemons[index]));
})
app.get(config.rootApi + "pokemons",function (req,res){
res.json(success(pokemons));
})
//app.use(config.rootAPI + "pokemons", PokemonsRouter);
app.listen(config.port, ()=> console.log('Attrapez les tous !'))
let app = express() function getIndex(id){
app.use(express.static('../frontend'))
app.listen(8080); return (pokemons.map((a) => a.id).indexOf(parseInt(id)));
console.log('Server started') }
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error 404</title>
</head>
<body>
<div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%);">
<img src="error.gif" alt="none">
<p>Error <%=title%> Not found !</p>
</div>
</body>
</html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment