-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (45 loc) · 2.17 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
'use strict';
// SERVER CONFIGURATION ---------------------------------------------------------------------------
require('dotenv').config();
const express = require('express');
const app = express();
const addAsteroidToLiked = require('./src/like-asteroid/add-asteroid-to-liked');
const getAsteroidComparison = require('./src/asteroid-comparison/get-asteroid-comparison');
const getAsteroidDataFromAPI = require('./src/nasa-asteroid-api/data-fetch/get-asteroid-data-from-api');
const getLikedAsteroids = require('./src/liked-asteroids/get-liked-asteroids');
const getAllAsteroids = require('./src/all-asteroids/get-all-asteroids');
const updateAsteroidName = require('./src/update-asteroid/update-asteroid-name');
// Use EJS for embedding JS in html
app.set('view engine', 'ejs');
// connection to database
const PORT = process.env.PORT;
// Express middleware
// Utilize ExpressJS functionality to parse the body of the request
app.use(express.urlencoded({ extended: true }));
app.use(express.static('./public'));
app.listen(PORT, () => console.log(`Astro-Paranoid listening on ${PORT}`));
// ------------------------------------------------------------------------------------------------
// Route/endpoint handling
// Homepage, gets asteroids for today
app.get('/', getAsteroidDataFromAPI);
// more details page, shows asteroid scale, map, and info
app.get('/location/:id', getAsteroidComparison);
// liked page, has all the liked asteroids by every user
app.get('/liked', getLikedAsteroids);
// all page, all the asteroids in the database
app.get('/all', getAllAsteroids);
// adds asteroid to liked table in database
app.post('/likeAsteroid', addAsteroidToLiked);
// updates the name of the asteroid
app.post('/update/:id/:name', updateAsteroidName);
// Page directs ---------------------------------------------------------------
// about page, information about the developers
app.get('/about', (request, response) => {
response.render('./pages/about');
});
// application info page, purpose/goal of the application
app.get('/info', (request, response) => {
response.render('./pages/info');
});
//error handler for invalid endpoint
app.use('*', (req, res) => res.render('./pages/error'));