-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·118 lines (97 loc) · 3.7 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';
var express = require('express'),
path = require('path'),
httpProxy = require('http-proxy'),
favicon = require('serve-favicon'),
publicPath = path.resolve(__dirname, 'public'),
bodyParser = require('body-parser'),
isProduction = process.env.NODE_ENV === 'production',
port = isProduction ? process.env.PORT : 3000,
multiparty = require('connect-multiparty'),
helmet = require('helmet'),
fetch = require('isomorphic-fetch'),
mongoose = require('mongoose'),
Twitter = require('twitter');
if (!isProduction){
var config = require('./config.js');
}
var UserController = require('./db/controllers/userController.js'),
ListingController = require('./db/controllers/listingController.js');
var proxy = httpProxy.createProxyServer({
changeOrigin: true
});
var app = express();
app.use(express.static(publicPath));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(helmet());
app.use(multiparty());
app.use(favicon(__dirname + '/public/assets/black-house.ico'));
require('./server/S3ListingsMiddleware.js')(app);
require('./server/S3AvatarMiddleware.js')(app);
mongoose.connect(process.env.mongoURI || config.mongo_URI);
var client = new Twitter({
consumer_key: process.env.twitter_consumer_key || config.twitter_consumer_key,
consumer_secret: process.env.twitter_consumer_secret || config.twitter_consumer_secret,
access_token_key: process.env.twitter_access_token_key || config.twitter_access_token_key,
access_token_secret: process.env.twitter_access_token_secret || config.twitter_access_token_secret
});
app.route('/email')
.post(function(req, res, err) {
fetch("https://api.sendgrid.com/v3/mail/send", {
"method": "POST",
"headers": {
"authorization": process.env.sendgridAuth || config.sendgridAuth,
"content-type": "application/json",
"cache-control": "no-cache",
"postman-token": process.env.sendgridToken || config.sendgridToken
},
"body": JSON.stringify(req.body)
}).then((response, req) => {
res.status(response.status).send(response)});
});
app.route('/twitter')
.get(function(req, res){
var params = {
q: 'hacker houses',
count: 30
};
client.get('https://api.twitter.com/1.1/search/tweets.json', params, function(error, tweets, response) {
if (!error) {
res.send(tweets);
}
});
})
app.route('/v1/users')
.post(UserController.signUpUser)
.get(UserController.checkAuth, UserController.getAllUsers);
app.route('/v1/users/signin')
.post(UserController.signInUser);
app.route('/v1/users/:userId')
.get(UserController.getUserById)
.put(UserController.checkAuth, UserController.updateUser);
app.route('/v1/listings')
.get(ListingController.getAllListings)
.post(UserController.checkAuth, ListingController.createListing);
app.get('/v1/listings/:userId', UserController.checkAuth, ListingController.getListingsById);
app.get('/v1/listings/city/:city', ListingController.getListingsByCity);
app.get('/v1/listings/house/:house_name', ListingController.getListingsByName);
//server/compiler.js runs webpack-dev-server which creates the bundle.js which index.html serves
//will not see a physical bundle.js because webpack-dev-server runs it from memory
if (!isProduction) {
var bundle = require('./server/compiler.js');
bundle();
app.all('/build/*', function(req, res) {
proxy.web(req, res, {
target: 'http://localhost:8080'
});
});
}
proxy.on('error', function(e) {
console.log('Could not connect to proxy, please try again...');
});
app.listen(port, function() {
console.log('Server running on port ' + port);
});