-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
74 lines (56 loc) · 2.47 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
70
71
72
73
74
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
//var usersRouter = require('./routes/users');
const booksRouter = require('./routes/books'); // Modularizing the books-related routes
const db = require('./models/index'); // Importing the instance of 'sequelize' that was instantiated in the models/index.js file
// Testing the connection to the database and synchronizing the model
(async () => {
await db.sequelize.sync();
try {
await db.sequelize.authenticate();
console.log('The connection to the database was successful!'); // Connection successful
} catch (error) {
console.error('An error occurred when connecting to the database: ', error); // Connection not successful
}
})();
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use( '/static', express.static(path.join(__dirname, 'public')) ); // Defining a '/static' path for express.static (not specified in the instructions)
// Home route
app.use('/', indexRouter);
// Books Router (books-related routes)
app.use('/', booksRouter);
/******* ERROR HANDLERS *******/
// Handling 404 errors
app.use((req, res, next) => {
const err = new Error();
err.status = 404;
err.message = "Page not found. Check details of the error on the screen.";
console.log(err.message, "Error status:", err.status); // Displaying error message and status on the console (not specified in the instructions)
res.status(err.status); // Setting the response status to the error status (not specified in the instructions)
res.render('page-not-found', {err, title: "Page Not Found"});
next(err);
});
// Global error handler
app.use((err, req, res, next) => {
if (err.status === 404) {
return;
} else {
err.status = err.status || 500;
err.message = err.message || "Server error. Check the Error Stack below for more details.";
console.log("Error status:", err.status, err.message);
res.status(err.status); // Setting the response status to the error status, although it wasn't directly mentioned in the instructions
res.render('error', {err});
}
});
module.exports = app;