-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (49 loc) · 2.03 KB
/
index.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
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var expressJWT = require('express-jwt');
var request = require('request');
var jwt = require('jsonwebtoken');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname,'public')));
app.use(express.static(path.join(__dirname,'bower_components')));
var secret = process.env.SUPER_SECRET;
var mongoose = require('mongoose');
var User = require('./models/user');
mongoose.connect(process.env.MONGOLAB_URI || 'mongodb://localhost/beervana');
// app.use('/api/beers', expressJWT({secret: secret}));
app.use('/api/users', expressJWT({secret: secret})
.unless({path: ['/api/users'], method: 'post'}))
app.use('/api/users', require('./controllers/users'));
app.post('/api/auth', function(req, res) {
User.findOne({email: req.body.email}, function(err, user) {
if (err || !user) return res.status(401).send({message: 'User not found'});
user.authenticated(req.body.password, function(err, result) {
if (err || !result) return res.status(401).send({message: 'User not authenticated'});
var token = jwt.sign(user, secret);
res.send({user: user, token: token});
});
});
});
app.get('/api/beers', function(req, res) {
var key = process.env.BEER_API_KEY;
var searchTerm = req.query.q
request('https://api.brewerydb.com/v2/search?q='+searchTerm+'&type=beer&withBreweries=y&key='+key, function(err, response, data) {
console.log(JSON.parse(data).data)
res.send(JSON.parse(data).data)
});
});
app.get('/api/brewerys', function(req, res) {
var key = process.env.BEER_API_KEY;
var searchTerm = req.query.q
request('https://api.brewerydb.com/v2/search?q='+searchTerm+'&type=brewery&key='+key, function(err, response, data) {
console.log(JSON.parse(data).data)
res.send(JSON.parse(data).data)
});
});
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
app.listen(process.env.PORT || 3000);