-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathserver.js
61 lines (48 loc) · 1.5 KB
/
server.js
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
var express = require("express");
var app = express();
var characters = require("./data/characters.json");
var spells = require("./data/spells.json");
/*
Remove the X-Powered-By HTTP header
https://www.npmjs.com/package/hide-powered-by
*/
app.disable('x-powered-by');
app.use("/api", function (req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
next();
});
app.get("/", function (req, res) {
res.sendfile("public/index.html");
});
app.get("/api/characters", function (req, res) {
res.json(characters);
});
app.get("/api/character/:id", function (req, res) {
var id = req.params.id.toLowerCase();
res.json(
characters.filter((character) => character.id.toLowerCase() === id)
);
});
app.get("/api/characters/students", function (req, res) {
res.json(characters.filter((character) => character.hogwartsStudent));
});
app.get("/api/characters/staff", function (req, res) {
res.json(characters.filter((character) => character.hogwartsStaff));
});
app.get("/api/characters/house/:house", function (req, res) {
var house = req.params.house.toLowerCase();
res.json(
characters.filter((character) => character.house.toLowerCase() === house)
);
});
app.get("/api/spells", function(req, res){
res.json(spells);
});
app.use(express.static("public"));
app.set("port", (process.env.PORT || 5000));
app.listen(app.get("port"), function() {
console.log("Node app is running on port", app.get("port"));
});
app.use((req, res, next) => {
res.status(404).send("Sorry can't find that!")
})