-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (40 loc) · 1.26 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
37
38
39
40
41
42
43
44
45
46
47
48
49
const config = require('./utils/config');
const express = require('express');
const middleware = require('./utils/middleware');
const mongoose = require('mongoose');
const cors = require('cors');
const entriesRouter = require('./controllers/entries');
const usersRouter = require('./controllers/users');
const loginRouter = require('./controllers/login');
const imagesRouter = require('./controllers/file-upload');
var path = require('path');
app = express();
app.use(express.json());
app.use(middleware.tokenExtractor);
mongoose
.connect(config.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
})
.then(() => {
console.log('connected to MongoDB');
})
.catch((error) => {
console.log('error connection to MongoDB:', error.message);
});
app.use(cors());
app.use(express.static('build'));
app.use('/api/login', loginRouter);
app.use('/api/users', usersRouter);
app.use('/api/entries', entriesRouter);
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '/build/index.html'));
});
app.use(middleware.unknownEndpoint);
app.use(middleware.errorHandler);
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});