generated from jbohde/handlebars-express-sequelize-mysql
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
85 lines (76 loc) · 2.26 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
const express = require('express');
const exphbs = require('express-handlebars');
const path = require('path');
const session = require('express-session');
const SequelizeStore = require('connect-session-sequelize')(session.Store);
const { SafeString } = require('handlebars');
const sequelize = require('./db/config');
const routes = require('./routes');
const PORT = process.env.PORT || 3001;
const app = express();
const hbs = exphbs.create({
helpers: {
renderStars: rating => {
const stars = '⭑'.repeat(rating);
return new SafeString(stars);
},
// Helper to check if the user is the owner of the item and wether the item is available
isCurrentUserItem: function (
itemUserId,
currentUserId,
isAvailable,
options
) {
if (itemUserId === currentUserId) {
return options.inverse(this);
} else if (!isAvailable) {
return options.inverse(this);
} else {
return options.fn(this);
}
},
// Helper to shorten the description of an item
trimString: function (passedString) {
var theString = passedString.substring(0, 125);
return new SafeString(theString);
},
// Helper to help debug handlebars when iterating through an object
stringify: function (context) {
return JSON.stringify(context);
},
// Helper to check if two values are equal
ifEqual: function (value1, value2, options) {
if (value1 === value2) {
return options.fn(this);
} else {
return options.inverse(this);
}
},
},
runtimeOptions: {
allowProtoPropertiesByDefault: true,
allowProtoMethodsByDefault: true,
},
});
const sessionConfig = {
secret: process.env.SESSION_SECRET,
cookie: {},
resave: false,
saveUninitialized: true,
store: new SequelizeStore({
db: sequelize,
}),
};
app.use(session(sessionConfig));
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(routes);
const force = process.env.FORCE_SYNC === 'true';
sequelize.sync({ force }).then(() => {
app.listen(PORT, () => {
console.info(`Server listening on port ${PORT}`);
});
});