-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
80 lines (65 loc) · 1.9 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require('dotenv').config();
// setting up express Server
const express = require('express');
const path = require('path');
const axios = require("axios");
const overview = require("./routes/a-overview");
const income = require("./routes/a-income");
const balance = require("./routes/a-balance");
const cashflow = require("./routes/a-cashflow");
const health = require("./routes/a-sectorhealth");
const crypto = require("./routes/a-crypto-exchange");
const companySearch = require("./routes/company/search");
const companyChartData = require("./routes/company/chart-data");
const cors = require('cors');
const Mongoose = require('mongoose');
const app = express();
const router = express.Router();
// Connect to the MongoDB Database
app.use(cors());
app.use(express.static("client"));
// ! Connecting to the MongoDB Database
const db = 'mongodb+srv://user:[email protected]/<dbname>?retryWrites=true&w=majority'
const connectDB = async () => {
try {
await Mongoose.connect(db, {useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false})
console.log("MongoDB Connected...");
} catch (err) {
console.error(err.message);
}
}
connectDB();
// Initialize universal middleware
app.use(express.json({ extended: false }));
// to avoid cors error, give permission ot front end
app.use(
cors({
origin: 'http://localhost:8080',
methods:'GET,HEAD,PUT,PATCH,POST,DELETEGET,HEAD,PUT,PATCH,POST,DELETE',
credentials: true,
}),
);
app.use('/api/users',
require('./routes/api/users')
)
// Define Routes
app.use('/api/',
overview,
income,
balance,
cashflow,
health,
crypto,
);
app.use('/company/',
companySearch,
companyChartData,
)
app.use((req, res, next, err) => {
if (err) {
console.log(`ERROR at use: ${err}`);
}
});
// Setting up the port
const PORT = 3000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));