|
2 | 2 |
|
3 | 3 | const express = require("express") |
4 | 4 | const v1 = express.Router() |
5 | | -const error = {error: "No results found"} |
6 | 5 |
|
7 | 6 | // Get most recent launch |
8 | | -v1.get("/latest", (req, res) => { |
| 7 | +v1.get("/latest", (req, res, next) => { |
9 | 8 | global.db.collection("launch").find({},{"_id": 0 }).sort({"flight_number": -1}).limit(1) |
10 | 9 | .toArray((err, doc) => { |
11 | | - if (err) return console.log(err) |
12 | | - res.end(JSON.stringify(doc, null, 2)) |
| 10 | + if (err) { |
| 11 | + return next(err) |
| 12 | + } |
| 13 | + res.json(doc) |
13 | 14 | }) |
14 | 15 | }) |
15 | 16 |
|
16 | 17 | // All launches by date, year, or default to all launches |
17 | | -v1.get("/", (req, res) => { |
| 18 | +v1.get("/", (req, res, next) => { |
18 | 19 | const year = req.query.year |
19 | 20 | const start = req.query.start |
20 | 21 | const final = req.query.final |
21 | 22 | const site = req.query.site |
22 | 23 | if (year) { |
23 | 24 | global.db.collection("launch").find({"launch_year": `${year}`}, {"_id": 0 }).sort({"flight_number": -1}) |
24 | 25 | .toArray((err, doc) => { |
25 | | - if (doc.length == 0) { |
26 | | - res.status(200).end(JSON.stringify(error, null, 2)) |
27 | | - } |
28 | | - res.end(JSON.stringify(doc, null, 2)) |
| 26 | + res.json(doc) |
29 | 27 | }) |
30 | 28 | } else if (start && final) { |
31 | 29 | global.db.collection("launch").find({ "launch_date_utc": {"$gte": `${start}T00:00:00Z`, "$lte": `${final}T00:00:00Z`}}, {"_id": 0 }) |
32 | 30 | .sort({"flight_number": 1}) |
33 | 31 | .toArray((err, doc) => { |
34 | | - if (doc.length == 0) { |
35 | | - res.status(200).end(JSON.stringify(error, null, 2)) |
36 | | - } |
37 | | - res.end(JSON.stringify(doc, null, 2)) |
| 32 | + res.json(doc) |
38 | 33 | }) |
39 | 34 | } else if (site) { |
40 | 35 | global.db.collection("launch").find({ "launch_site.site_id": `${site}`}, {"_id": 0 }) |
41 | 36 | .sort({"flight_number": 1}) |
42 | 37 | .toArray((err, doc) => { |
43 | | - if (doc.length == 0) { |
44 | | - res.status(200).end(JSON.stringify(error, null, 2)) |
45 | | - } |
46 | | - res.end(JSON.stringify(doc, null, 2)) |
| 38 | + res.json(doc) |
47 | 39 | }) |
48 | 40 | } else { |
49 | 41 | global.db.collection("launch").find({},{"_id": 0 }).sort({"flight_number": 1}) |
50 | 42 | .toArray((err, doc) => { |
51 | | - if (err) return console.log(err) |
52 | | - res.end(JSON.stringify(doc, null, 2)) |
| 43 | + if (err) { |
| 44 | + return next(err) |
| 45 | + } |
| 46 | + res.json(doc) |
53 | 47 | }) |
54 | 48 | } |
55 | 49 | }) |
56 | 50 |
|
57 | 51 | // Returns launches by core serial # |
58 | | -v1.get("/cores/:core", (req, res) => { |
| 52 | +v1.get("/cores/:core", (req, res, next) => { |
59 | 53 | const core = req.params.core |
60 | 54 | global.db.collection("launch").find({"core_serial": `${core}`},{"_id": 0}).sort({"core_serial": 1}) |
61 | 55 | .toArray((err, doc) => { |
62 | | - if (err) return console.log(err) |
63 | | - if (doc.length == 0) { |
64 | | - res.status(200).end(JSON.stringify(error, null, 2)) |
| 56 | + if (err) { |
| 57 | + return next(err) |
65 | 58 | } |
66 | | - res.end(JSON.stringify(doc, null, 2)) |
| 59 | + res.json(doc) |
67 | 60 | }) |
68 | 61 | }) |
69 | 62 |
|
70 | 63 | // Returns launches by capsule serial # |
71 | | -v1.get("/caps/:cap", (req, res) => { |
| 64 | +v1.get("/caps/:cap", (req, res, next) => { |
72 | 65 | const cap = req.params.cap |
73 | 66 | global.db.collection("launch").find({"cap_serial": `${cap}`},{"_id": 0}).sort({"capsule_serial": 1}) |
74 | 67 | .toArray((err, doc) => { |
75 | | - if (err) return console.log(err) |
76 | | - if (doc.length == 0) { |
77 | | - res.status(200).end(JSON.stringify(error, null, 2)) |
| 68 | + if (err) { |
| 69 | + return next(err) |
78 | 70 | } |
79 | | - res.end(JSON.stringify(doc, null, 2)) |
| 71 | + res.json(doc) |
80 | 72 | }) |
81 | 73 | }) |
82 | 74 |
|
83 | 75 | // Returns all ASDS launches |
84 | | -v1.get("/asds", (req, res) => { |
| 76 | +v1.get("/asds", (req, res, next) => { |
85 | 77 | global.db.collection("launch").find({"landing_type": "ASDS"},{"_id": 0}).sort({"flight_number": 1}) |
86 | 78 | .toArray((err, doc) => { |
87 | | - if (err) return console.log(err) |
88 | | - res.end(JSON.stringify(doc, null, 2)) |
| 79 | + if (err) { |
| 80 | + return next(err) |
| 81 | + } |
| 82 | + res.json(doc) |
89 | 83 | }) |
90 | 84 | }) |
91 | 85 |
|
92 | 86 | // Returns all RTLS launches |
93 | | -v1.get("/rtls", (req, res) => { |
| 87 | +v1.get("/rtls", (req, res, next) => { |
94 | 88 | global.db.collection("launch").find({"landing_type": "RTLS"},{"_id": 0}).sort({"flight_number": 1}) |
95 | 89 | .toArray((err, doc) => { |
96 | | - if (err) return console.log(err) |
97 | | - res.end(JSON.stringify(doc, null, 2)) |
| 90 | + if (err) { |
| 91 | + return next(err) |
| 92 | + } |
| 93 | + res.json(doc) |
98 | 94 | }) |
99 | 95 | }) |
100 | 96 |
|
|
0 commit comments