Last active
August 22, 2017 13:01
-
-
Save brittanydionigi/19275a98b183466955408b595457d3ef to your computer and use it in GitHub Desktop.
Trains Express setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ================================================================= | |
// require all necessary packages ================================== | |
// ================================================================= | |
const express = require('express'); | |
const app = express(); | |
const bodyParser = require('body-parser'); | |
const jwt = require('jsonwebtoken'); | |
// ================================================================= | |
// app setup & configuration ======================================= | |
// ================================================================= | |
app.set('port', 3001); | |
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' } | |
]; | |
app.use(bodyParser.json()); | |
// ================================================================= | |
// API Endpoints =================================================== | |
// ================================================================= | |
app.get('/api/v1/trains', (request, response) => { | |
response.status(200).json(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.status(404); } | |
const originalTrain = app.locals.trains[index]; | |
app.locals.trains[index] = Object.assign(originalTrain, train); | |
return response.status(200).json(app.locals.trains); | |
}); | |
// ================================================================= | |
// start the server ================================================ | |
// ================================================================= | |
app.listen(app.get('port'), () => { | |
console.log(`Listening on http://localhost:${app.get('port')}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment