-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
50 lines (41 loc) · 1.42 KB
/
app.js
File metadata and controls
50 lines (41 loc) · 1.42 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const express = require('express');
const app = express();
const mongoose = require('mongoose');
// use for autocreate API documentation
const swaggerUi = require('swagger-ui-express')
const swaggerFile = require('./swagger_output.json')
const errorHandler = require('./_helpers/error_handler');
require('dotenv/config'); //permits access to .env/
const bodyParser = require('body-parser');
const cors = require('cors')
//middelwares -> is it execute each time a route is called
//app.use(auth);
app.use(cors()); //this permits cross domain request
app.use(bodyParser.json());
//import routes
const postsRoute = require('./routes/posts')
const stationsRoute = require('./routes/stations')
const usersRoute = require('./routes/users');
//ROUTES
app.get('/', (req, res)=> {
res.send("we are on home");
});
app.use('/posts', postsRoute);
app.use('/stations', stationsRoute);
app.use('/user', usersRoute);
app.use('/doc', swaggerUi.serve, swaggerUi.setup(swaggerFile))
// global error handler
app.use(errorHandler);
//connect to db
mongoose.connect(process.env.DB_CONNECTION, {
useNewUrlParser: true,
})
.catch(err => { //if there are any errors...
console.error('DB Connection error:', err.stack)
throw err
})
.then(() => {
console.log("Connection to MongoDB successfully!")
});
//how to we start listening to the server
app.listen(process.env.PORT);