-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·39 lines (29 loc) · 1.04 KB
/
main.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
'use strict'
const express = require("express");
var cors = require('cors');
const mongoose = require("mongoose");
const morgan = require("morgan");
const app = express();
const handler = require("./middleware/Handler");
app.use(cors());
app.options('*', cors());
// setting up db connection
mongoose.Promise = Promise;
mongoose.set('useFindAndModify', false);
global.URI = "mongodb://localhost:27017/sandbox";
mongoose.connect(global.URI, { useNewUrlParser: true });
var db = mongoose.connection;
db.on("error", function(err){ console.error("DB Connection Error: ", err);});
db.once("open", function(){ console.log("DB Connected.");});
// 3rd Party Middlewares
app.use(express.json()); //Decodes Form Data
app.use(morgan('tiny')); //Logs requests
// API Route
const link = require("./routes/link");
app.use("/api/link", link);
// middleware to handle err and res.
app.use(handler.handleRes);
app.use(handler.handleError);
// Starting Server
const port = process.env.PORT || 3000;
app.listen(port, function(){console.log("URL Shortener API | ", port);});