-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
38 lines (26 loc) · 921 Bytes
/
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
const express = require('express');
const passport = require('passport');
const bodyParser = require('body-parser');
const booksRoute = require('./routes/books');
const authRoute = require('./routes/auth');
require('./db').connectToMongoDB() // Connect to MongoDB
require('dotenv').config()
require("./authentication/auth") // Signup and login authentication middleware
const PORT = 3000;
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/', authRoute);
app.use('/books', passport.authenticate('jwt', { session: false }), booksRoute);
// renders the home page
app.get('/', (req, res) => {
res.send('Welcome to the book API');
});
// Handle errors.
app.use(function (err, req, res, next) {
console.log(err);
res.status(err.status || 500);
res.json({ error: err.message });
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`)
})