-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
135 lines (118 loc) · 4 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const
dotenv = require('dotenv').load(),
express = require('express'),
logger = require('morgan'),
mongoose = require('mongoose'),
app = express(),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
mongoDBURL = 'mongodb://localhost/tripply',
ejsLayouts = require('express-ejs-layouts'),
tripsRoutes = require('./routes/trips.js'),
usersRoutes = require('./routes/users.js'),
session = require('express-session'),
MongoDBStore = require('connect-mongodb-session')(session),
flash = require('connect-flash'),
passport = require('passport'),
passportConfig = require('./config/passport.js'),
methodOverride = require('method-override'),
yelp = require('yelp-fusion'),
request = require('request'),
argv = require('yargs').argv,
moment = require('moment'),
clientId = process.env.CLIENT_ID,
clientSecret = process.env.CLIENT_SECRET,
yelpKey = process.env.API_Key
//weather api
const apiKey = '43e17d975597a344bebe6fabf0eefaaa'
const
PORT = process.env.PORT || 3002,
mongoConnectionString = process.env.MONGODB_URI || 'mongodb://localhost/tripply'
mongoose.connect(mongoConnectionString, (err) => {
console.log(err || 'Connected to MongoDB')
})
const store = new MongoDBStore({
uri: mongoConnectionString,
collection: 'sessions'
})
app.set('view engine', 'ejs')
app.use(logger('dev'))
app.use(bodyParser.json())
app.use(cookieParser())
app.use(ejsLayouts)
app.use(bodyParser.urlencoded({ extended: true }))
app.use(flash())
app.use(express.static(`${__dirname}/public`))
app.use(methodOverride('_method'))
app.use(session({
secret: 'ssshhhhhh',
cookie: {maxAge: 26280000000},
resave: true,
saveUninitialized: false,
store: store
}))
app.use(passport.initialize())
app.use(passport.session())
app.use((req, res, next) => {
app.locals.currentUser = req.user
app.locals.loggedIn = !!req.user
app.locals.showFooter = true
next()
})
app.get('/', (req, res) => {
//res.render({message: "The root."})
res.render('../views/home', {showFooter: false})
})
//weather
app.get('/weather', function (req, res) {
res.render('weather', {weather: null, error: null});
})
app.get('/weather/:id', function(req, res) {
console.log(req.params)
let locale = req.params.id
let url = `http://api.openweathermap.org/data/2.5/weather?q=${locale}&units=imperial&appid=${apiKey}`
request(url, function (err, response, body) {
// res.send(body)
res.render('../views/weather',{ weather: JSON.parse(body) })
})
})
//
// app.get('/search', (req, res) => {
// console.log(req)
// yelp.accessToken(clientId, clientSecret).then(response => {
// const client = yelp.client(response.jsonBody.access_token)
// // console.log(client)
// client.search({
// term: req.query.term,
// location: req.query.location
// }).then(response => {
// console.log(response.jsonBody.businesses);
// res.json(response.jsonBody.businesses)
// // res.render('../views/try', {results: response.jsonBody.businesses})
// })
// }).catch(e => {
// console.log(e);
// });
// })
app.get('/search', (req, res) => {
// console.log(res)
// yelp.accessToken(clientId, clientSecret).then(response => {
// const client = yelp.client(response.jsonBody.access_token)
const client = yelp.client(yelpKey)
// // console.log(client)
client.search({
term: req.query.term,
location: req.query.location
}).then(response => {
// console.log(response.jsonBody.businesses);
res.json(response.jsonBody.businesses)
// res.render('../views/try', {results: response.jsonBody.businesses})
}).catch(e => {
console.log(e);
});
})
app.use('/trips', tripsRoutes)
app.use('/users', usersRoutes)
app.listen(PORT, (err) => {
console.log(err || `Server connected on port ${PORT}`)
})