-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
47 lines (39 loc) · 1.33 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
require('dotenv').config();
const express = require('express');
const expressLayouts = require('express-ejs-layouts');
const fileUpload = require('express-fileupload');
const session = require('express-session');
const cookieParser = require('cookie-parser');
const flash = require('connect-flash');
const methodOverride = require('method-override');
const path = require('path');
const app = express();
const port = process.env.PORT || 3000;
// Middleware setup
app.use(express.urlencoded({ extended: true }));
app.use(express.json()); // Handles JSON bodies
app.use(express.static(path.join(__dirname, 'public')));
app.use(expressLayouts);
app.use(methodOverride('_method')); // Method overriding
app.use(cookieParser('CookingBlogSecure'));
app.use(session({
secret: 'CookingBlogSecretSession',
saveUninitialized: true,
resave: true
}));
app.use(flash());
app.use(fileUpload());
// View engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('layout', './layouts/main'); // Layout path
app.set('view engine', 'ejs');
// Routes setup
const routes = require('./server/routes/recipeRoutes.js');
app.use('/', routes);
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});
// Start server
app.listen(port, () => console.log(`Listening on port ${port}`));