-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
49 lines (42 loc) · 1.32 KB
/
Copy pathapp.js
File metadata and controls
49 lines (42 loc) · 1.32 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');
const routes = require('./src/routes/index');
require('dotenv').config();
const cors = require("cors");
const bodyParser = require('body-parser');
const path = require('path')
const app = express();
const dbUrl = process.env.DB_URL;
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
res.header(
"Access-Control-Expose-Headers",
"Access-Token, Uid, x-pagination-total-count, x-pagination-page-count, x-pagination-start-record, x-pagination-end-record"
);
next();
});
mongoose.connect(dbUrl, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log('Connected to MongoDB');
})
.catch((err) => {
console.error('Error connecting to MongoDB:', err);
});
app.use('/api', routes);
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
const PORT =7000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});