-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
40 lines (32 loc) · 1.2 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
const express = require('express');
const mongoose = require('mongoose');
const passport = require('passport');
const bodyParser = require('body-parser');
require('./auth/auth');
mongoose.connect('mongodb+srv://root:[email protected]/myFirstDatabase?retryWrites=true&w=majority');
mongoose.connection.on('error', error => console.log(error) );
mongoose.Promise = global.Promise;
const routes = require('./routes/routes');
const userRoutes = require('./routes/user-routes');
const categoryRoutes = require('./routes/category-routes');
const cors = require('cors');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors({
origin: '*',
methods: ['GET', 'HEAD', 'PUT', 'POST', 'DELETE']
}));
app.use('/', routes);
// Plug in the JWT strategy as a middleware so only verified users can access this route.
app.use('/users', passport.authenticate('jwt', { session: false }), userRoutes);
app.use('/categories', categoryRoutes);
// Handle errors.
app.use(function(err, req, res, next) {
console.log(err)
res.status(err.status || 500);
res.json({ error: err });
});
app.listen(3001, () => {
console.log('Server started.')
});