-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (50 loc) · 1.96 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
const express = require('express');
const bunyan = require('bunyan');
const debug = require('debug');
const bodyParser = require('body-parser');
const dbConfig = require('./config/database.config.js');
const mongoose = require('mongoose');
// var mongodb = require('mongodb');
// var MongoClient = require('mongodb').MongoClient;
const cors = require('cors');
const app = express();
app.use(cors());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// parse requests of content-type - application/json
app.use(bodyParser.json());
mongoose.Promise = global.Promise;
const logger = bunyan.createLogger({name: "Server Log"});
var myLogger = (req,res,next)=>{
logger.info(`new request on : ${ Date.now()}`);
next();
};
app.use(myLogger);
const options = {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
autoIndex: false, // Don't build indexes
reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
reconnectInterval: 500, // Reconnect every 500ms
poolSize: 10, // Maintain up to 10 socket connections
// If not connected, return errors immediately rather than waiting for reconnect
bufferMaxEntries: 0,
connectTimeoutMS: 10000, // Give up initial connection after 10 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
family: 4 // Use IPv4, skip trying IPv6
};
// Connecting to the database
mongoose.connect(dbConfig.url, options).then(() => {
console.log("Successfully connected to the database");
}).catch(err => {
console.log('Could not connect to the database. Exiting now...', err);
process.exit();
});
app.get('/',(req,res)=> res.send("Hello world "));
require('./routes/post.routes.js')(app);
process.on('uncaughtException',(err)=>{
logger.error(' Caught exception: ${err}');
});
let port= process.env.port || 3005
app.listen(port,'localhost',()=> debug('listening ${ port }'));