-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
85 lines (69 loc) · 2.07 KB
/
app.js
File metadata and controls
85 lines (69 loc) · 2.07 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
// Require the express module
const express = require('express');
//setting up swagger for documentation
const swaggerUi = require('swagger-ui-express');
const swaggerJSDoc = require('swagger-jsdoc');
const app = express();
const session = require('express-session');
const cors = require('cors');
// Set port
const port = 3000;
// Configure Swagger
// Swagger definition
const swaggerOptions = {
swaggerDefinition: {
openapi: '3.0.0',
info: {
title: 'My API',
version: '1.0.0',
description: 'API documentation using Swagger',
},
servers: [
{
url: `http://localhost:${port}`,
},
],
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
},
},
apis: ['./routes/*.js'], // Path to your API docs
};
const swaggerDocs = swaggerJSDoc(swaggerOptions);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));
// Set EJS as the view engine
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cors({
origin: 'http://localhost:3000', // Your frontend origin
credentials: true // Allow cookies / credentials
}));
// Serve static files from the 'public' directory
app.use(express.static('public'));
app.use(session({
secret: 'cats', // Change this to a random string
resave: false,
saveUninitialized: false,
cookie: { maxAge: 3600000 } // Session expires after 1 hour
}));
app.use((req, res, next) => {
res.locals.isLoggedIn = req.session.username ? true : false;
next();
});
// Require and use the router module
const router = require('./routes/router').router;
app.use('/', router);
app.use('/guesthome', router);
app.use('/register', router);
// Start the server
const server = app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
module.exports = { app, server }; // Export the app and server objects for testing