forked from Aryan-kumar-mains/iNotebook-MernProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (39 loc) · 1.47 KB
/
index.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
58
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import bodyParser from 'body-parser';
import path from 'path';
// components
import Routes from "./routes/routes.js";
import Connection from "./db.js";
const __dirname = path.resolve();
dotenv.config({ path: './.env' });
console.log(process.env.JWT_SECRET);
Connection(); // for connecting to mongodb via mongoose
const app = express();
app.use(express.json()); //for using req.body
// Available Routes
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.use("/", Routes);
// For deployment : To run React after deployment
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, '/client/build')));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
// Global error handling middleware
app.use((err, req, res, next) => {
err.statusCode = err?.statusCode || 500;
err.status = err?.status || 'error';
res.status(err.statusCode).json({
status: err.status,
error: err,
message: err.message,
stack: err.stack
})
});
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`iNotebook Server is successfully running on PORT ${port}`));
//install nodemon by writing e.g. npm i -D nodemon, -D for devDependencies so that it'l not be a part of our project