-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
92 lines (73 loc) · 2.03 KB
/
Copy pathapp.js
File metadata and controls
92 lines (73 loc) · 2.03 KB
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
const express = require('express');
const app = express();
const static = express.static(__dirname + '/public');
const session = require('express-session');
const configRoutes = require('./routes');
const exphbs = require('express-handlebars');
app.use('/public', static);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
var hbs = exphbs.create({});
hbs.handlebars.registerHelper('each_upto', function(ary, max, options) {
if(!ary || ary.length == 0)
return options.inverse(this);
var result = [ ];
for(var i = 0; i < max && i < ary.length; ++i)
result.push(options.fn(ary[i]));
return result.join('');
});
const handlebarsInstance = exphbs.create({
defaultLayout: 'main',
// Specify helpers which are only registered on this instance.
helpers: {
asJSON: (obj, spacing) => {
if (typeof spacing === 'number')
return new Handlebars.SafeString(JSON.stringify(obj, null, spacing));
return new Handlebars.SafeString(JSON.stringify(obj));
}
},
partialsDir: ['views/partials/']
});
app.engine('handlebars', handlebarsInstance.engine);
app.set('view engine', 'handlebars');
app.use('/recipes/edit/:id', async (req, res, next) => {
if(req.method==='POST'){
req.method='PATCH';
}
next();
});
app.use('/recipes/delete/:id', async (req, res, next) => {
if(req.method==='GET'){
req.method='DELETE';
}
next();
});
app.use('/follow/:id', async (req, res, next) => {
if(req.method === 'POST'){
req.method = 'PATCH';
}
next();
})
app.use('/saveRecipe/:id', async (req, res, next) => {
if(req.method === 'POST'){
req.method = 'PATCH';
}
next();
})
app.use(session({
name: 'AuthCookie',
secret: 'some secret string!',
resave: false,
saveUninitialized: true
}));
app.use('/editUser', (req, res, next) =>{
if(req.method == 'POST'){
req.method = 'PATCH';
}
next();
})
configRoutes(app);
app.listen(3000, () => {
console.log("We've now got a server!");
console.log('Your routes will be running on http://localhost:3000');
});