-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
62 lines (53 loc) · 1.5 KB
/
app.js
File metadata and controls
62 lines (53 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const cors = require("cors");
const express = require("express");
const config = require("./config");
const app = express()
const Pokemon = require("./models/pokemon.js")
const bodyParser = require ('body-parser');
app.use(bodyParser.json());
app.use(cors({ origin: "http://localhost:4200" }));
app.get(["/pokemon", "/"], (req, res) => {
Pokemon.find().then(pokemons => {
res.send( pokemons );
}).catch((err) => {
res.status(500).send(err)
});
})
app.post("/pokemon", (req,res) => {
let newPokemon = new Pokemon(
req.body
);
newPokemon.save().then( pokemon => {
res.status(201).send(pokemon);
}).catch((err) => {
res.status(500).send(err);
});
});
app.delete("/pokemon/:id", (req, res) => {
const id = req.params;
Pokemon.findOneAndDelete({id}, (err, pokemon) => {
if(err) {
res.status(500).send()
} else if (!pokemon) {
res.status(404).send()
} else {
res.send("deleted");
}
})
});
// Pokemon.insertMany().then(function () {
// console.log("Data inserted") // Success
// }).catch(function (error) {
// console.log(error) // Failure
// });
app.get("/pokemon/:id", (req, res) => {
const id = req.params.id
Pokemon.findOne({id}).then(pokemon => {
res.send( pokemon );
}).catch((err) => {
res.status(500).send(err)
});
})
app.listen(config.port, () => {
console.log(`Server running at ${config.host}:${config.port}`);
});