-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
36 lines (27 loc) · 1.07 KB
/
Copy pathserver.js
File metadata and controls
36 lines (27 loc) · 1.07 KB
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
const express = require('express'),
app = express(),
morgan = require('morgan'),
bodyParser = require('body-parser'),
mongoose = require('mongoose');
const posts = require('./api/routes/posts');
const configMlab = {
username: 'GuiFSs',
password: 'guilherme10'
};
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
mongoose.connect(`mongodb://${configMlab.username}:${configMlab.password}@ds139725.mlab.com:39725/microposts`);
mongoose.Promise = global.Promise;
const port = 5000;
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*'); // CORS handling, * means every site can access my API
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-with, Content-type, Accept, Authorization');
if(req.method === 'OPTIONS'){
res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET');
return res.status(200).json({});
}
next();
});
app.use('/api/posts', posts);
app.listen(port, () => console.log(`Listening on port ${port}`));