-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheshop.mjs
More file actions
86 lines (73 loc) · 2.72 KB
/
eshop.mjs
File metadata and controls
86 lines (73 loc) · 2.72 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
import * as dotenv from 'dotenv'; // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import { logStartUpDetailsToConsole } from './middleware/index.js';
import { restrictToAdmin } from './middleware/index.js';
import bodyParser from 'body-parser';
import session from 'express-session';
import winston from 'winston';
// Server Initialization
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Logging
// https://github.com/winstonjs/winston
// https://betterstack.com/community/guides/logging/how-to-install-setup-and-use-winston-and-morgan-to-log-node-js-applications/
const logger = winston.createLogger({
level: process.env.LOG_LEVEL || 'info',
format: winston.format.combine(winston.format.timestamp(), winston.format.prettyPrint()),
defaultMeta: { service: 'eshop' },
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'eshop.log' }),
],
});
logger.info('Hello world!');
logger.warn('Warning message');
logger.debug('Debugging info');
logger.error('Error message');
// Set EJS as the template engine
app.set('view engine', 'ejs');
// Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, "public")));
// Specify the directory where your views/templates are located
app.set('views', path.join(__dirname, 'views'));
// Session variables
//app.set('trust proxy', 1) // trust first proxy
const oneDay = 1000 * 60 * 60 * 24;
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: { maxAge: oneDay }
}));
app.use((req, res, next) => {
res.locals.session = req.session;
// res.set('Content-Security-Policy',
// "default-src 'self'; frame-ancestors 'self'; form-action 'self';");
next();
});
// Local Modules
import homeRoute from './routes/homeRoute.mjs';
import productRoute from './routes/product.mjs';
import categoryRoute from './routes/category.mjs';
import userRoute from './routes/user.mjs';
import cartRoute from './routes/cart.mjs';
import orderRoute from './routes/order.mjs';
import admin from './routes/admin.mjs';
// Routes will be written here
app.use('/', homeRoute);
app.use('/', productRoute);
app.use('/', categoryRoute);
app.use('/', userRoute);
app.use('/', cartRoute);
app.use('/', orderRoute);
app.use('/admin', restrictToAdmin, admin);
app.listen(port, logStartUpDetailsToConsole(app, port));
export default logger;