forked from amazon-archives/aws-summit-hackathon-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
50 lines (40 loc) · 1.37 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
'use strict'
const express = require('express')
const bodyParser = require('body-parser')
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')
const app = express()
app.set('view engine', 'pug')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(awsServerlessExpressMiddleware.eventContext())
app.use(express.static('static'));
app.get('/', (req, res) => {
res.render('index')
})
app.get('/hotels', (req, res) => {
// Get all of hotel information
res.json(hotels)
})
app.post('/hotel/:hotelId', (req, res) => {
// Reserve a hotel at the given index
let ix = getHotelIndex(req.params.hotelId)
hotels[ix].availability = hotels[ix].availability - 1
res.status(201).json(hotels)
})
const getHotel = (hotelId) => hotels.find(h => h.id === parseInt(hotelId))
const getHotelIndex = (hotelId) => hotels.findIndex(h => h.id === parseInt(hotelId))
// Mock data for example. Must use real database for production
const hotels = [{
id: 1,
name: 'Mars Motel',
availability: 10
}, {
id: 2,
name: 'Space Time International',
availability: 10
}]
// The aws-serverless-express library creates a server and listens on a Unix
// Domain Socket for you, so you can remove the usual call to app.listen.
// app.listen(3000)
// Export your express server so you can import it in the lambda function.
module.exports = app