-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.js
87 lines (71 loc) · 1.95 KB
/
app.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
76
77
78
79
80
81
82
83
84
85
86
87
const path = require('path');
const express = require('express');
const OS = require('os');
const bodyParser = require('body-parser');
const mongoose = require("mongoose");
const app = express();
const cors = require('cors')
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, '/')));
app.use(cors())
mongoose.connect(process.env.MONGO_URI, {
user: process.env.MONGO_USERNAME,
pass: process.env.MONGO_PASSWORD,
useNewUrlParser: true,
useUnifiedTopology: true
}, function(err) {
if (err) {
console.log("error!! " + err)
} else {
// console.log("MongoDB Connection Successful")
}
})
var Schema = mongoose.Schema;
var dataSchema = new Schema({
name: String,
id: Number,
description: String,
image: String,
velocity: String,
distance: String
});
var planetModel = mongoose.model('planets', dataSchema);
app.post('/planet', function(req, res) {
// console.log("Received Planet ID " + req.body.id)
planetModel.findOne({
id: req.body.id
}, function(err, planetData) {
if (err) {
alert("Ooops, We only have 9 planets and a sun. Select a number from 0 - 9")
res.send("Error in Planet Data")
} else {
res.send(planetData);
}
})
})
app.get('/', async (req, res) => {
res.sendFile(path.join(__dirname, '/', 'index.html'));
});
app.get('/os', function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.send({
"os": OS.hostname(),
"env": process.env.NODE_ENV
});
})
app.get('/live', function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.send({
"status": "live"
});
})
app.get('/ready', function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.send({
"status": "ready"
});
})
app.listen(3000, () => {
console.log("Server successfully running on port - " +3000);
})
module.exports = app;