-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (41 loc) · 1.03 KB
/
index.js
File metadata and controls
49 lines (41 loc) · 1.03 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
const express = require("express");
const mongoose = require("mongoose");
require("dotenv").config();
/**
* @type {number}
*/
const port = process.env.PORT || 3000; // Port to run server on
/**
* @type {string}
*/
const mongodbUri = process.env.MONGODB_URI; // MongoDB URI
/**
* @returns {Promise<void>}
*/
const start = async () => {
try {
// Try to connect to mongodb
const connect = await mongoose.connect(mongodbUri);
console.log("CONNECTED TO MONGODB: ", connect.connection.db.databaseName);
// Create express app
const app = express();
// Create routes
app.get("/", async (req, res) => {
res.status(200).json({
message: "Server is running",
});
});
// Start server
app.listen(port, () => {
console.log(`SERVER STARTED ON http://localhost:${port}`);
});
} catch (error) {
// If error, log it
console.error("SERVER ERROR: ", error);
// Exit process
process.exit(1);
}
};
start()
.then(() => console.log("SERVER IS STARTED SUCCESSFULLY"))
.catch((error) => console.log("SERVER ERROR", error));