-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
54 lines (41 loc) · 1.68 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
53
54
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const helmet = require('helmet');
const autoIncrement = require('mongoose-auto-increment');
const app = express();
mongoose.connect('mongodb+srv://Vikas:' + process.env.MONGO_ATLAS_PW +'@todolist-rjoaq.mongodb.net/todo-list?retryWrites=true', { autoIndex: false, useNewUrlParser: true })
.then(() => {
console.log('successfully connected to mongodb!!!');
})
.catch((err) => {
console.dir(err);
console.log('connection to mongodb failed!!!');
});
autoIncrement.initialize(mongoose.connection);
//routes
const userRoutes = require('./routes/user-routes');
const listRoutes = require('./routes/list-routes');
const itemRoutes = require('./routes/item-routes');
const itemHistoryRoutes = require('./routes/item-history-routes');
app.use(helmet());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin','*');
res.setHeader('Access-Control-Allow-Headers',"Origin, X-Requested-With, Content-Type, Accept, Authorization");
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PATCH, PUT, DELETE, OPTIONS');
next();
});
app.use('/api/user', userRoutes);
app.use('/api/list', listRoutes);
app.use('/api/item', itemRoutes);
app.use('/api/item-history', itemHistoryRoutes);
app.use((err, req, res, next) => {
let status = err.status || 500;
res.status(status).send(err);
})
app.use('*', (req, res, next) => {
res.status(404).send({message: 'The route you are trying to access is not valid!!!'});
})
module.exports = app;