-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·54 lines (44 loc) · 1.67 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
let express = require('express');
let mongoose = require('mongoose');
let morgan = require('morgan');
let bodyParser = require('body-parser');
let logger = require('logger').createLogger('development.log');
var path = require('path');
let routes = require('./routes');
let app = express();
let connectToDb = () =>{
// mongoose.connect('mongodb://127.0.0.1:27017/lostAndFound');
mongoose.connect('mongodb+srv://summy:[email protected]/lostAndFound?retryWrites=true', { useNewUrlParser: true });
mongoose.connection.once('open',()=>{
console.log('DB CONNECTION SUCCESSFUL');
}).on('error',(error)=>{
console.log('DB CONNECTION FAILED (check whether database is running !), error object : ', error);
});
}
let startServer = () =>{
// Middlewears
app.use(morgan('dev'));
app.use(bodyParser.json());
// console.log('$$$ process.env: ', process.env);
// console.log('###PORT NUMBER process.env.PORT :::: ', process.env.PORT);
if(!process.env.PORT){
console.log('over riding port number');
process.env.PORT = 8001;
}
// Routing configurations
app.use(express.static(__dirname + '/dist/public'));
// API & Controller requests
app.use('/api',routes);
// Wild Card
app.get('*', function(req, res){
res.sendFile(__dirname + '/dist/public/index.html');
});
// Connection to db
connectToDb();
// Starting Server
app.listen(process.env.PORT ,"0.0.0.0", () =>{
logger.info(`server start on port ${process.env.PORT} `);
console.log(`Server is now running at port ${process.env.PORT} in localhost`);
});
}
startServer();