-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
57 lines (46 loc) · 1.73 KB
/
server.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
/**
* main Javascript file for the application
* this file is executed by the Node server
*/
// import the http module, which provides an HTTP server
const http = require("http");
// import the express module, which exports the express function
const express = require("express");
// invoke the express function to create an Express application
const app = express();
const server = http.createServer(app);
// create a new web socket server object
const { createSocketServer } = require("./server/socket/socket");
createSocketServer(server);
// load environment variables from the .env file into process.env
const dotenv = require("dotenv");
dotenv.config({ path: ".env" });
// connect to MongoDB using mongoose
const mongoose = require("mongoose");
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log("Connected to MongoDB");
})
.catch((err) => {
console.error("MongoDB connection error:", err);
});
// add middleware to handle JSON in HTTP request bodies (used with POST commands)
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// set the template engine to EJS, which generates HTML with embedded JavaScript
app.set("view engine", "ejs");
// load assets
app.use("/css", express.static("assets/css"));
app.use("/img", express.static("assets/img"));
app.use("/js", express.static("assets/js"));
app.use("/html", express.static("assets/html"));
// to keep this file manageable, we will move the routes to a separate file
// the exported router object is an example of middleware
app.use("/", require("./server/routes/router"));
// start the server on port 8081
server.listen(8081, () => {
console.log("server is listening on http://localhost:8081");
});