-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (48 loc) · 1.83 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
46
47
48
49
50
51
52
53
54
55
56
57
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const paginate = require('express-paginate');
const config = require('./config');
const postRoutes = require('./routes/post');
const imageUploadRoutes = require('./routes/images');
const userRoutes = require('./routes/users');
const infoRoutes = require('./routes/info');
const commentsRoutes = require('./routes/comments');
const app = express();
mongoose.connect(
`mongodb+srv://${config.DB_USER}:${config.DB_SECRET}@${config.DB_HOST}/${config.DB_NAME}?retryWrites=true&w=majority`,
{
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
}
);
//db.posts.createIndex( { "title": "text", "details": "text", "county": "text", "stories":"text", "suggestedFunctions": "text", "comments": "text"} )
var allowlist = [
'http://localhost:8080',
'https://patrimonioesquecido.ruilebre.com',
'https://lostheritage.ruilebre.com'
];
var corsOptionsDelegate = function (req, callback) {
var corsOptions;
if (allowlist.indexOf(req.header('Origin')) !== -1) {
corsOptions = { origin: true }; // reflect (enable) the requested origin in the CORS response
} else {
corsOptions = { origin: false }; // disable CORS for this request
}
callback(null, corsOptions); // callback expects two parameters: error and options
};
app.use(cors(corsOptionsDelegate));
app.use(cors());
app.use(paginate.middleware(10, 50)); // limit, maxLimit
app.use(bodyParser.json());
app.use('/api/v1/posts', postRoutes);
app.use('/api/v1/image', imageUploadRoutes);
app.use('/api/v1/users', userRoutes);
app.use('/api/v1/infos', infoRoutes);
app.use('/api/v1/comments', commentsRoutes);
app.listen(process.env.PORT || 3000, function () {
console.log('Server running');
});