-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (43 loc) · 1.27 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
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const chatRoutes = require('./routes/chat');
const socketServer = require('./socket/server');
dotenv.config();
const MONGODB_URI = `mongodb+srv://powlinett:${process.env.MONGODB_PASSWORD}@node-chat-cluster.aqnmb.mongodb.net/node-chat?retryWrites=true&w=majority`
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.set('view engine', 'ejs');
app.set('views', 'views');
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'public')));
app.use(chatRoutes);
app.use('/', (req, res, next) => {
try {
res.render('no-page', {pageTitle: 'No page yet'});
} catch (err) {
console.log(err);
}
});
socketServer.connect(io);
const launchServer = async () => {
try {
await mongoose.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
});
http.listen(port);
} catch (err) {
console.log(err);
}
};
launchServer();