forked from codeforsanjose/disaster-response-sj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
70 lines (58 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
66
67
68
69
70
import express from 'express'
import bodyParser from 'body-parser'
import bcrypt from 'bcrypt'
import fs from 'fs'
import session from 'express-session'
import _ from 'lodash'
import errorHandler from 'errorhandler'
import passport from 'passport'
import path from 'path'
import cookieParser from 'cookie-parser'
import { posts_db_name } from './backend/Utilities/API_utilities'
import { db } from './backend/lib/db'
import { init } from './backend/lib/auth'
import { postsRouter } from './backend/routes/postsRoutes'
import { usersRouter } from './backend/routes/userRoutes'
const app = express()
const publicDir = __dirname + '/public'
const MessagingResponse = require('twilio').twiml.MessagingResponse;
app.use(bodyParser({limit: '4MB'}))
app.use(bodyParser.json());
app.set('port', process.env.PORT || 8080)
app.use((req, res, next) =>{
res.setHeader('Access-Control-Allow-Origin', '*, disaster-response.s3-website-us-west-1.amazonaws.com');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, DELETE, OPTIONS');
next();
});
app.use('/public', express.static('public'))
app.use(cookieParser())
app.use(session({ secret: 'keyboard cat', resave: false, saveUninitialized: false }))
app.use(postsRouter)
app.use(usersRouter)
init(app)
app.get(['/', '/login'], (req, res) => {
res.sendFile(path.join(publicDir, '/index.html'))
})
app.post('/api/login', (req, res, next) => {
// See: https://github.com/jaredhanson/passport-local
passport.authenticate('local', (err, user, info) => {
if (err || !user) {
req.user = user
console.log('error with login:', err, user)
return res.status(422).json(err)
}
req.login(user, () => {
return res.json(user)
})
})(req, res, next)
})
app.post('/sms', (req, res) => {
const twiml = new MessagingResponse();
twiml.message('SMS works!');
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
});
app.listen(app.get('port'), function () {
console.log('[*] disaster response running on port', app.get('port'))
})