-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
78 lines (61 loc) · 1.91 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
75
76
77
78
const express = require('express');
const dotenv = require('dotenv');
const morgan = require('morgan');
const exphbs = require('express-handlebars');
const colors = require('colors');
const cookieParser = require('cookie-parser');
const errorHandler = require('./middleware/error');
const connectDB = require('./config/db');
//route files
const getRoutes = require('./routes/index');
const products = require('./routes/products');
const ingredients = require('./routes/ingredients');
const categories = require('./routes/categories');
const auth = require('./routes/auth');
const users = require('./routes/users');
//Use .env file in config folder
dotenv.config({ path: './config/config.env' });
connectDB();
const app = express();
//body parser
app.use(express.json());
//loggers/ messages that allow you to return
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
//Handlebars
app.engine(
'.hbs',
exphbs.engine({
//this is the defaut layout passed every time
defaultLayout: 'main',
extname: '.hbs',
}),
);
// doing this will set for handle bars to look for a folder called 'views'
app.set('view engine', '.hbs');
//Static Folder
app.use(express.static('public'));
app.use(express.static(__dirname + '/node_modules/bootstrap-icons/'));
app.use(express.static(__dirname + '/node_modules/bootstrap/'));
//Mount routers
app.use('/v1/', getRoutes);
app.use('/', products);
app.use('/v1/ingredients', ingredients);
app.use('/v1/categories', categories);
app.use('/v1/auth', auth);
app.use('/v1/users', users);
app.use(errorHandler);
const PORT = process.env.PORT || 4000;
app.listen(
PORT,
console.log(
`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`.yellow,
),
);
// Handle unhandled promise rejections
process.on('unhandled rejection', (err, promise) => {
console.log(`Error ${err.message}.red`);
//close server &exis process
server.close(() => process.exit(1));
});