-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
44 lines (36 loc) · 987 Bytes
/
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
// express app
const express = require('express');
const path = require('path');
const app = express();
const cors = require('cors');
const port = 5000;
// .env access
const dotenv = require('dotenv');
dotenv.config({ path: '.env' });
// Database connection
const dbConnection = require('./config/db');
dbConnection();
// JSON parse
app.use(express.json());
app.use(express.static('views'));
// Enable other domains to access the app
app.use(cors());
app.options('*', cors());
// Routes
const quotesRouter = require('./routes/quotes');
// Mount Routes
app.use('/api/v1/quote', quotesRouter);
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.all('*', (req, res, next) => {
res.status(404).json(`Can't find ${req.originalUrl} on this server!`);
});
// Server
app.listen(process.env.PORT || port, function () {
console.log(
`Express server listening on port ${this.address().port} in ${
app.settings.env
} mode`
);
});