-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (27 loc) · 1.03 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
const express = require('express');
const app = express();
require("dotenv").config();
const port = process.env.PORT || 5000;
const mongoose = require('mongoose');
const expressJwt = require('express-jwt');
const path = require('path');
app.use(express.json());
app.use(express.static(path.join(__dirname, 'client', 'build')));
app.use('/api', expressJwt({secret: process.env.SECRET, algorithms: ['HS256']}));
app.use('/api', require('./routes/apiRoutes'));
app.use('/auth', require('./routes/authRoutes'));
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/noted', {useNewUrlParser: true}).then(() => {
console.log('Connected to MongoDB')
}).catch(err => console.log(err));
app.use((err, req, res, next) => {
if(err.name === 'UnauthorizedError'){
res.status(err.status);
}
return res.send({message: err.message});
})
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'))
});
app.listen(port, () => {
console.log(`server is running on port ${port}`)
})