-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
40 lines (33 loc) · 1.37 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
// MONGO user: manny
// MONGO password for user: 14M48nVzC5AbDwsm
// MONGO Connection: mongodb+srv://manny:<password>@cluster0-dmszo.mongodb.net/test?retryWrites=true&w=majority
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const path = require('path');
//Routes
const stuffRoutes = require('./routes/stuff');
const userRoutes = require('./routes/user');
const app = express();
//connecting to database
mongoose.connect('mongodb+srv://manny:14M48nVzC5AbDwsm@cluster0-dmszo.mongodb.net/test?retryWrites=true&w=majority')
.then(() => {
console.log('Successfully connected to MongoDB Atlas!')
})
.catch((error) => {
console.log('Unable to connect to MongoDB Atlas!');
console.error(error);
});
//avoiding CORS(Cross Origin Resource Sharing), this adds to the headed
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content, Accept, Content-Type,' +
' Authorization');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
next();
});
app.use(bodyParser.json());
app.use('/images', express.static(path.join(__dirname, 'images')));
app.use('/api/stuff', stuffRoutes);
app.use('/api/auth', userRoutes);
module.exports = app;