-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
67 lines (48 loc) · 1.73 KB
/
Copy pathserver.js
File metadata and controls
67 lines (48 loc) · 1.73 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const app = express();
//swagger deps
const swaggerUi = require('swagger-ui-express');
const yaml = require('yamljs');
//setup swagger
const swaggerDefinition = yaml.load('./swagger.yaml');
app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(swaggerDefinition));
const teamRoutes = require("./routes/team");
const playerRoutes = require("./routes/player");
const authRoutes = require("./routes/auth");
const newsRoutes = require("./routes/news");
require ("dotenv-flow").config();
//parse request of content type JSON
app.use(bodyParser.json());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS, POST, PUT, DELETE"); //If using .fetch not axios
res.header("Access-Control-Allow-Headers", "auth-token, Origin, X-Requested-Width, Content-Type, Accept");
next();
})
mongoose.connect
(
process.env.DBHOST,
{
useUnifiedTopology: true,
useNewUrlParser: true
}
).catch(error=>console.log("Error connecting to MongoDB:" +error));
mongoose.connection.once('open', ()=>console.log('Connected successfuly to MongoDB'));
//routes
app.get("/api/welcome", (req, res) => {
res.status(200).send({message: "Welcome to the MEN RESTful API"});
})
// post, put, delete -> CRUD
app.use("/api/teams", teamRoutes);
app.use("/api/players", playerRoutes);
app.use("/api/user", authRoutes);
app.use("/api/news", newsRoutes);
// /api/user/register
// /api/user/login
const PORT = process.env.PORT || 4000;
app.listen(PORT, function() {
console.log("Server is running on port: " +PORT);
})
module.exports = app;