-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
56 lines (48 loc) · 1.5 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
const express = require('express');
const app = express();
require('dotenv').config();
const bodyParser = require('body-parser');
const morgan = require('morgan');
const mongoose = require('mongoose');
// const expressJwt = require('express-jwt');
const path = require('path');
const PORT = process.env.PORT || 7250;
app.use(express.json());
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'client', 'build')));
mongoose.connect(process.env.DB_URI || 'mongodb://localhost:27017/quotesDB',
{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
// useFindandModify: false
},
(err) => {
if (err) throw err;
console.log('Connected to the database');
}
);
app.use('/auth', require('./routes/auth'))
app.use('/api/quotes', require('./routes/quoteRouter.js'));
// app.use('api/quotes/character', require('./routes/quoteRouter.js'));
app.use(function(req, res) {
res.status(404).send({url: req.originalUrl + ' not found'})
});
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', './client/public/index.html'),
function(err) {
if (err) {
res.status(500).send(err)
}
});
});
app.use((err, req, res, next) => {
if (err.name === 'UnauthorizedError') {
res.status(err.status)
}
return res.send({ message: err.message });
});
app.listen(PORT, () => {
console.log(`[+] Starting server on port ${PORT}`)
});