-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (38 loc) · 1.24 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
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const indexRouter = require('./routes/index');
const loginRouter = require('./routes/login');
const usersRouter = require('./routes/users');
const jwtVerificationMiddleware = require('./middlewares/jwt-verification');
mongoose
.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
poolSize: 10,
})
.then(() => {
console.log('Connected to database');
// Heroku listens on environment variable PORT automatically. They set this up internally
app.listen(process.env.PORT);
})
.catch((error) => {
console.error('Could not connect to database', error.message);
});
const app = express();
app.use(express.json());
app.use(
cors({
origin: ['http://localhost:4200', 'https://codewithcoffee.netlify.app'],
credentials: true, // needed for jwt headers
})
);
app.use('/', indexRouter);
app.use('/login', loginRouter);
app.use('/users', jwtVerificationMiddleware, usersRouter);
app.use(function errorHandler(error, req, res, next) {
res.status(error.statusCode || 500).json({ error: error.message });
});