-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathapp.js
54 lines (44 loc) · 1.26 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const cors = require("cors");
// Import Creds
const mongo = require("./credentials/mongo");
// Imports for Routes
const todoRoutes = require("./routes/todo");
// Create an Express App
const app = express();
// Handle MongoDB Connection
mongoose
.connect(mongo.localConnString, {
useNewUrlParser: true
})
.then(() => {
console.log("Connected to database!");
})
.catch(() => {
console.log("Connection failed!");
});
// Use body-parser to parse incoming reuests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
// Use Cors to avoid annoying CORS Errors
app.use(cors());
// Handle Authentication If Any
// Send basic info about the API
app.use("/api/info", (req, res, next) => {
res.status(200).json({
name: "TODO Api",
version: "1.0",
description: "RESTful API Designed in Node.js for TODO application.",
methodsAllowed: "GET, POST, PUT, PATCH, DELETE",
authType: "None",
rootEndPoint: req.protocol + '://' + req.get('host') + '/api/v1',
documentation: "https://github.com/toslimarif/todo-api"
});
});
// Set up API Routes
app.use("/api/v1/todo", todoRoutes);
module.exports = app;