-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
52 lines (44 loc) · 1.71 KB
/
app.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
// External package imports
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const cors = require('cors');
const mongoose = require('mongoose');
const passport = require('passport');
require('dotenv').config();
// Internal imports
require('./auth/passport');
// Router imports
const entriesRouter = require('./routes/entries');
const usersRouter = require('./routes/users');
const friendsRouter = require('./routes/friends');
const journeysRouter = require('./routes/journeys');
const commentsRouter = require('./routes/comments');
const app = express();
// MongoDB
const mongoDB = process.env.MONGODB_KEY;
mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false });
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
// App-level Middleware
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors({
'origin': '*',
'methods': 'GET, HEAD, PUT, PATCH, POST, DELETE'
}));
// Routers
app.use('/entries', passport.authenticate('jwt', { session: false }), entriesRouter);
app.use('/friends', passport.authenticate('jwt', { session: false }), friendsRouter);
app.use('/journeys', passport.authenticate('jwt', { session: false }), journeysRouter);
app.use('/comments', passport.authenticate('jwt', { session: false }), commentsRouter);
app.use('/users', usersRouter);
// App test
app.get('/', function (req, res) {
res.send('Welcome to the social media API. This app is up and running.');
});
module.exports = app;