-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (41 loc) · 1.64 KB
/
server.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
//import dependencies
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');
const app = express();
//load configuration from .env file
require('dotenv-flow').config();
//setup Swagger
const swaggerDocument = YAML.load('./swagger.yaml');
app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
//import routes and validation
const authRoutes = require("./routes/auth");
const productRoutes = require("./routes/product");
// middleware defitions
// parse requests of content-type - application/json
app.use(bodyParser.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
//connect to the MongoDB using Mongoose ODM
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 succesfully to MongoDB'));
//routes definition
//Welcome route
app.get("/api/welcome", (req,res) => {
res.status(200).send({message: "Welcome to the MEN-RESTful-API"});
});
// authentication routes to secure the API endpoints
app.use("/api/user", authRoutes); //authentication routes (register, login)
app.use("/api/products", productRoutes); //CRUD routes
// setup and connect to our DB
console.log(process.env.DBHOST);
//start up server
const PORT = process.env.PORT || 4000;
app.listen(PORT, function () {
console.log("Server is running on port: " + PORT);
});
module.exports = app;