-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
37 lines (31 loc) · 2.11 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
const mongoose = require("mongoose");
const dotenv = require("dotenv");
dotenv.config({ path: "./config.env" });
const app = require("./app");
const DB_NAME = require("./constants");
//!Catching uncaught exceptions like console.log(x) but x is not defined yo synchronous tala ko i guess async
process.on("uncaughtException", (err) => {
console.log("Uncaught execption shuutting down....");
console.log(err.name, err.message);
process.exit(1);
});
//!DATABASE CONNECTED
const DB = `${process.env.DATABASE}/${DB_NAME}`;
mongoose.connect(DB, {}).then(() => console.log("DB connected Successfully")); //In summary, the order of log messages depends on the execution time of asynchronous operations. Both operations (database connection and server listening) happen concurrently, and the order of their completion determines the order of the log message
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Listening at ${port}`); //In summary, the order of log messages depends on the execution time of asynchronous operations. Both operations (database connection and server listening) happen concurrently, and the order of their completion determines the order of the log message
});
//In server.js, when you call app.listen(port, ...) to start the server, it will listen for incoming requests and automatically run through the middleware stack for each request.
//Each middleware function specified with app.use() or app.all() will be executed in the order in which they are defined. The request will pass through each middleware function, and each function has the opportunity to modify the request or response or perform other actions before passing control to the next middleware or route handler.
//!Handling uncaught erroer like db not connected and all DB DOWN FOR SOME REASON
process.on("unhandledRejection", (err) => {
console.log(err.name, err.message);
console.log("UNHANDLED REJECTION Shutting Down");
server.close(() => {
//!SERVER.CLOSE LE ABBORTLY DIRECTLY CLOSE GARDAINA BISTARI GARXA.GOOD PRATICE TO USE
process.exit(1);
});
});
// console.log(process.env);
console.log(process.env.NODE_ENV);