-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
69 lines (57 loc) · 2.06 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
// Include packages needed for this application
const express = require('express');
const session = require('express-session');
// Imports the routing files from ./routes directory
const routes = require('./routes');
// const bodyParser = require('body-parser');
const path = require('path');
// import sequelize connection
const sequelize = require('./config/connection');
const morgan = require('morgan');
require('dotenv').config();
// set port the server will listen to
const PORT = process.env.PORT || 3000;
// initializes a new instance of the Express application
const app = express();
// Session middleware setup
app.use(session({
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: false,
cookie: {
secure: 'auto',
httpOnly: true,
maxAge: 3600000 // Example: expires in 1 hour
}
}));
//Setting view engine to EJS
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// express app to recognize incoming requests as JSON objects
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
// Route to serve the landing page
app.get('/', (req, res) => {
res.render('index');
});
//editor
app.use('/quill', express.static(path.join(__dirname, 'node_modules/quill/dist')));
app.use(morgan('tiny')); // Log every request to the console
// middleware function mwLogger
const mwLogger = (req, res, next) => {
console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`);
next();
};
app.use(mwLogger);
// express app to use the routes defined
app.use(routes);
// To verify if Sequelize is successfully connecting to your database
sequelize.authenticate()
.then(() => console.log('Database connected successfully.'))
.catch(err => console.error('Unable to connect to the database:', err));
sequelize.sync({ force: false }) // Consider using 'force: true' only in development
.then(() => {
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));
})
.catch(err => console.error('Sequelize sync error:', err));