-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·80 lines (74 loc) · 2.19 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const express = require('express');
const app = express();
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');
const cors = require('cors');
const passport = require('passport');
const mongoose = require('mongoose');
const config = require('./config/database');
const expressValidator = require('express-validator');
const flash = require('connect-flash');
const morgan = require('morgan');
const multer = require('multer');
const db = mongoose.connection;
mongoose.connect(config.name,{auth:{authdb:"UsaIndia"}});
// mongoose.connect(config.name,
// {
// server: {
// socketOptions: {
// connectTimeoutMS: 500
// }
// },function(err, db) {
// if (err) throw err;
// console.log("Database created!");
// }
// });
db.on("connected",() =>{
console.log("Database is connected"+config.name);
});
db.on("error",(err) =>{
console.log("Database is connected"+err);
});
db.on('disconnected', function() {
console.log('MongoDB disconnected!');
mongoose.connect(config.name, {server:{auto_reconnect:true}});
});
mongoose.set('debug', true);
app.use('/image',express.static(path.join(__dirname, 'api/templates/jobs_attachment')));
// Express Validator
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param : formParam,
msg : msg,
value : value
};
}
}));
//Connect Flash
app.use(flash());
// log every request to the console
app.use(morgan('dev'));
//api routes
app.use(cors());
//console.log(apis)
//parsers for post api
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
//app.use('/uploads', express.static('uploads'))
app.use('/uploads', express.static('uploads'));
require('./api/routes/main')(app);
const port = process.env.PORT || '3000';
app.set('port', port);
const server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port, () => console.log(`API running on localhost:${port}`));