This repository was archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (76 loc) · 2.68 KB
/
Copy pathserver.js
File metadata and controls
92 lines (76 loc) · 2.68 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
87
88
89
90
91
92
const express = require("express");
const session = require('express-session');
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require("passport");
const cors = require('cors');
const morgan = require('morgan');
require('dotenv').config()
const api_routes = require("./Routes/auth_routes");
const chat_routes = require("./Routes/chat_routes")
const { MONGO_URI, SERVER_PORT } = require("./configurations/constants");
const redisClient = require("./configurations/redis").RedisClient;
const app = express();
// Bodyparser middleware
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(cors());
app.use(morgan('dev'));
/**
* Configures session middleware with the given options.
*
* @param {object} options - The options to use when configuring session middleware.
*/
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true,
// cookie: { secure: true }
}));
/**
* Initializes Passport authentication middleware.
*/
app.use(passport.initialize());
/**
* Uses Passport middleware to maintain persistent login sessions.
*/
app.use(passport.session());
/**
* Configures Passport authentication middleware with the given Passport instance.
*
* @param {object} passport - The Passport instance to configure.
*/
require("./configurations/passport")(passport);
/**
* Opens a connection to the Redis server using the Redis client.
*
* @returns {Promise} A promise that resolves when the connection is successful, and rejects if there is an error.
*/
redisClient.connect();
/**
* Mounts the given API routes on the '/api/users' path.
*
* @param {string} path - The path to mount the routes on.
* @param {object} routes - The API routes to mount.
*/
app.use('/api/users', api_routes);
app.use('/api/chats', chat_routes);
/**
* Connects to a MongoDB database using the given URI and options.
*
* @param {string} uri - The URI of the MongoDB database to connect to.
* @param {object} options - The options to use when connecting to the database.
* @returns {Promise} A promise that resolves when the connection is successful, and rejects if there is an error.
*/
mongoose.connect(
MONGO_URI,
{ useNewUrlParser: true, useUnifiedTopology: true }
)
.then(() => console.log("MongoDB successfully connected"))
.catch(err => console.log(err));
/**
* Express application
* @typedef {Object} ExpressApp
* @property {function} listen - Starts the server and listens for requests on a specified port
*/
app.listen(SERVER_PORT, () => console.log(`Server up and running on ${process.env.APP_URL}:${SERVER_PORT} !`));