generated from lighthouse-labs/node-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
116 lines (95 loc) · 3.59 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
// load .env data into process.env
require('dotenv').config();
// Web server config
const sassMiddleware = require('./lib/sass-middleware');
const express = require('express');
const morgan = require('morgan');
const cookieSession = require('cookie-session');
//
const productQueries = require('./db/queries/products');
const categoryQueries = require('./db/queries/categories');
const PORT = process.env.PORT || 8080;
const app = express();
app.use(cookieSession({
name: 'session',
keys: ["ILoveSecureThingsThey'reSoSecure"],
// Cookie Options
maxAge: 24 * 60 * 60 * 1000 // 24 hours
}));
app.set('view engine', 'ejs');
// Load the logger first so all (static) HTTP requests are logged to STDOUT
// 'dev' = Concise output colored by response status for development use.
// The :status token will be colored red for server error codes, yellow for client error codes, cyan for redirection codes, and uncolored for all other codes.
app.use(morgan('dev'));
app.use(express.urlencoded({ extended: true }));
app.use(
'/styles',
sassMiddleware({
source: __dirname + '/styles',
destination: __dirname + '/public/styles',
isSass: false, // false => scss, true => sass
})
);
app.use(express.static('public'));
// Separated Routes for each Resource
// Note: Feel free to replace the example routes below with your own
const userApiRoutes = require('./routes/users-api');
const widgetApiRoutes = require('./routes/widgets-api');
const usersRoutes = require('./routes/users');
const profileRoutes = require('./routes/profile');
const authRoutes = require('./routes/auth');
const productsRoutes = require('./routes/products');
const itemsRoutes = require('./routes/items');// const indexRoutes = require('./routes/index')
// Mount all resource routes
// Note: Feel free to replace the example routes below with your own
// Note: Endpoints that return data (eg. JSON) usually start with `/api`
app.use('/api/users', userApiRoutes);
app.use('/api/widgets', widgetApiRoutes);
app.use('/users', usersRoutes);
app.use('/auth', authRoutes);
app.use('/profile', profileRoutes);
app.use('/add-item', productsRoutes);
app.use('/items', itemsRoutes);
//app.use('/items')
// app.use('/', indexRoutes)
// Note: mount other resources here, using the same pattern above
// Home page
// Warning: avoid creating more routes in this file!
// Separate them into separate routes files (see above).
app.get('/', (req, res) => {
let dbquery;
const { min, max, category } = req.query;
if (min && max) {
dbquery = productQueries.filterProductByPriceWithCategoryName((min * 100), (max * 100));
} else if (category) {
dbquery = productQueries.filterProductByCategoryWithCategoryName(category);
} else {
dbquery = productQueries.getAllProductsWithCategoryName();
}
Promise.all([
dbquery.then((allArtData) => {
return allArtData;
}),
// categoryQueries.getCategoriesByProduct(6) // needs to dynamic from product <-- this will need to be on more than the profile page???
// .then(data => {
// const categoryData = data;
// console.log("---- category data", categoryData);
// return categoryData;
// }),
])
.then(data => {
allArtData = data[0];
categoryData = data[1];
const templateVars = { allArtData, categoryData, artist_id: req.session && req.session.artist_id };
console.log("testing templateVars", templateVars);
res.render('index', templateVars);
})
.catch(err => {
res
.status(500)
.json({ error: err.message });
});
});
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}`);
});