-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
101 lines (74 loc) · 2.99 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// =================================================================
// require all necessary packages & our .env config file ===========
// =================================================================
// require('dotenv').config()
const jwt = require('jsonwebtoken');
const express = require('express');
const cors = require('cors');
const app = express();
const bodyParser = require('body-parser');
const config = require('dotenv').config()
// =================================================================
// app setup & configuration =======================================
// =================================================================
app.locals.trains = [
{ id: 1, line: 'green', status: 'running' },
{ id: 2, line: 'blue', status: 'delayed' },
{ id: 3, line: 'red', status: 'down' },
{ id: 4, line: 'orange', status: 'maintenance' }
];
// Use body parser so we can get info from POST/URL parameters
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors());
if (!config.CLIENT_SECRET || !config.USERNAME || !config.PASSWORD) {
throw 'Make sure you have a CLIENT_SECRET, USERNAME, and PASSWORD in your env file'
}
app.set('secretKey', config.CLIENT_SECRET);
app.set('password', config.PASSWORD);
app.set('username', config.USERNAME);
// =================================================================
// API Endpoints ===================================================
// =================================================================
// This is all you baby!
// Authentication/Login Endpoint
app.post('/authenticate', (request, response) => {
const user = request.body;
console.log(user);
// If the user enters credentials that don't match our hard-coded
// credentials in our .env configuration file, send a JSON error
if (user.username !== config.USERNAME || user.password !== config.PASSWORD) {
response.status(403).send({
success: false,
message: 'Invalid Credentials'
});
}
// If the credentials are accurate, create a token and send it back
else {
let token = jwt.sign(user, app.get('secretKey'), {
expiresIn: 172800 // expires in 48 hours
});
response.json({
success: true,
username: user.username,
token: token
});
}
});
app.get('/api/v1/trains', (request, response) => {
response.send(app.locals.trains);
});
app.patch('/api/v1/trains/:id', (request, response) => {
const { train } = request.body;
const { id } = request.params;
const index = app.locals.trains.findIndex((m) => m.id == id);
if (index === -1) { return response.sendStatus(404); }
const originalTrain = app.locals.trains[index];
app.locals.trains[index] = Object.assign(originalTrain, train);
return response.json(app.locals.trains);
});
// =================================================================
// start the server ================================================
// =================================================================
app.listen(3001);
console.log('Listening on http://localhost:3001');