-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
52 lines (39 loc) · 1.16 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
const cors = require('cors');
const dotenv = require('dotenv');
const express = require("express");
const mongoose = require('mongoose')
const httpContext = require("express-http-context");
const notFound = require("./errors/notFound");
const errorHandler = require("./errors/errorHandler");
const path = require('path')
const router = require("./routes");
const corsOptions = {
origin: "*",
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
preflightContinue: false,
optionsSuccessStatus: 204,
};
// Load environment variables
dotenv.config();
// Create Express server
const app = express();
//public
app.use('/public',express.static('public'))
// Connecting Database
mongoose.connect(process.env.MONGO_URI,()=>{
app.listen(process.env.PORT);
console.log("App is running at http://localhost:%d ",process.env.PORT);
});
// Express configuration
app.use(express.json());
app.use(express.urlencoded({ extended: true, limit: "5mb" }));
// CORS configuration
app.use(cors(corsOptions));
app.options("*", cors);
// Set HTTP context
app.use(httpContext.middleware);
app.use(router);
// Error handling
app.use(errorHandler)
app.use(notFound);
module.exports = app;