-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (60 loc) · 1.69 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
62
63
64
65
66
67
const express = require('express');
const app = express();
const knex = require('knex');
const config = require('./knexfile');
const db = knex(config);
require('dotenv').config();
app.use(express.static('dist'));
app.get('/api/asteroids', async (req, res) => {
try {
const asteroids = await db.select().table('asteroids');
console.log(asteroids);
res.json(asteroids);
} catch (err) {
console.error('Error loading asteroids!', err);
res.sendStatus(500);
}
});
app.get('/api/asteroids/namesAndSize', async (req, res) => {
try {
const asteroidNames = await db
.select('name', 'diameter_in_meters_min')
.table('asteroids');
res.json(asteroidNames);
} catch (err) {
console.error('Error finding names!', err);
res.sendStatus(500);
}
});
app.get('/api/asteroids/:name', async (req, res) => {
const name = req.params.name;
try {
const asteroidInfo = await db.select().table('asteroids').where({
name: name,
});
res.json(asteroidInfo);
} catch (err) {
console.error('Error finding names!', err);
res.sendStatus(500);
}
});
app.get('/api/closest/:name', async (req, res) => {
const name = req.params.name;
try {
const diameterToCompare = await db
.select('diameter_in_meters_min')
.table('asteroids')
.where({
name: name,
});
let diameter = diameterToCompare[0]['diameter_in_meters_min'];
const compareable = await db.raw(
`select name, diameter_in_meters from items order by abs(diameter_in_meters - ${diameter}) limit 1;`
);
res.json(compareable.rows[0]);
} catch (err) {
console.error('Error finding names!', err);
res.sendStatus(500);
}
});
module.exports = app;