-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
118 lines (105 loc) · 3.12 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
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
const lib = require('lib')({ token: process.env.STDLIB_KEY });
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const passport = require('passport');
const LocalStrategy = require('passport-local');
const User = require('./models/User');
const Item = require('./models/Item');
const authRoutes = require('./routes/auth');
const itemRoutes = require('./routes/item');
const app = express();
const Promise = require('bluebird');
function seed(){
User.findOne({ username: 'bob' }, (err, user) => {
const item = {
name: 'hose',
price: '$4',
inprogress: false,
desc: 'hosey',
duration: 0,
offerer: {
id: user._id,
iid: user.iid,
username: user.username,
ready: false
},
}
const item2 = {
name: 'bike',
price: '$15',
inprogress: false,
desc: 'bikebikebike',
duration: 0,
offerer: {
id: user._id,
iid: user.iid,
username: user.username,
ready: false
},
}
const item3 = {
name: 'rake',
price: '$5',
inprogress: false,
desc: 'snake',
duration: 0,
offerer: {
id: user._id,
iid: user.iid,
username: user.username,
ready: false
},
}
Item.create(item, (err, item) => {
console.log(item);
});
Item.create(item2, (err, item) => {
console.log(item);
});
Item.create(item3, (err, item) => {
console.log(item);
});
});
}
// seed();
mongoose.connect(process.env.DB_URL);
app.use(express.static('public'));
app.use(express.static(__dirname + '/public'))
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.use(require("express-session")({
secret: "anything",
resave: false,
saveUninitialized: false
}));
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// passport initialization
app.use(passport.initialize());
app.use(passport.session());
app.use((req, res, next) => {
res.locals.currentUser = req.user;
next();
});
app.get('/list', (req, res) => {
res.render('catalog');
});
app.use(authRoutes);
app.use(itemRoutes);
app.get('/transactions', (req, res) => {
Item.find({}, (err, items) => {
let arr = [];
// couldnt get a proper OR query to work with mongoose :(
items.forEach(item => {
if ((item.borrower.username == req.user.username || item.offerer.username == req.user.username) && item.inprogress) {
arr.push(item);
}
});
res.render('transactions', { items: arr });
});
});
app.listen(process.env.PORT, process.env.IP, () => {
console.log(`listening on port ${process.env.PORT}`);
});