-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (66 loc) · 2.28 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var express = require('express');
var app = express();
let jsonFile = [
{
"id":"0",
"image":"gol_trendline.jpg",
"manufacturer":"Volkswagen",
"model":"Gol Trendline",
"engineSize":"80cm x 96cm",
"speed":"140 km/h",
"acceleration":"12 s"
},
{
"id":"1",
"image":"fox_pepper.png",
"manufacturer":"Volkswagen",
"model":"Fox Pepper",
"engineSize":"72cm x 94cm",
"speed":"170 km/h",
"acceleration":"11 s"
},
{
"id":"2",
"image":"citroen_c3.jpg",
"manufacturer":"Citroen",
"model":"C3",
"engineSize":"87cm x 56cm",
"speed":"185 km/h",
"acceleration":"7 s"
},
{
"id":"3",
"image":"citroen_c3_picasso.jpg",
"manufacturer":"Citroen",
"model":"C3 Picasso",
"engineSize":"73cm x 97cm",
"speed":"168 km/h",
"acceleration":"14 s"
}
];
app.get('/cars', function (req, res) {
res.setHeader('Content-Type', 'application/json');
res.header('Access-Control-Allow-Origin', '*');
var id = req.param('id');
if(id == null){
res.send(jsonFile);
}
if(id < 0){
res.send({'error':'Invalid ID'});
return;
}
for(var count = 0; count < jsonFile.length; count++){
if(jsonFile[count].id == id){
res.send(jsonFile[count]);
return;
}
}
res.send({'error':'Object not found!'});
})
app.get('/', function (req, res) {
res.send("HOME (:");
})
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});