-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
52 lines (44 loc) · 1.66 KB
/
app.js
File metadata and controls
52 lines (44 loc) · 1.66 KB
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
"use strict";
require('dotenv').config();
// Main starting point of the application
const express = require('express');
const http = require('http');
const bodyParser = require('body-parser'); // parse requests to JSON
const morgan = require('morgan'); // log-in incoming requests framework
const app = express();
const fs = require('fs');
const router = require('./router');
const mongoose = require('mongoose');
const cors = require('cors');
const env = process.env;
// create a write stream (in append mode)
let accessLogStream = null;
if (env.NODE_ENV !== "production") {
accessLogStream = fs.createWriteStream(__dirname + '/access.log',{flags: 'a'});
} else {
accessLogStream = fs.createWriteStream(env.OPENSHIFT_LOG_DIR + '/access.log',{flags: 'a'});
}
// DB Setup
mongoose.Promise = require('q').Promise;
if (env.NODE_ENV !== "production") {
mongoose.connect('mongodb://localhost:auth/smartpowersocket');
} else {
mongoose.connect('mongodb://admin:TlHr2YZHWSGg@127.12.161.2:27017/api');
// mongoose.connect('mongodb://admin:TlHr2YZHWSGg@$OPENSHIFT_MONGODB_DB_HOST:$OPENSHIFT_MONGODB_DB_PORT/api');
}
// App Setup - middleware
app.use(morgan('combined', {stream: accessLogStream}));
app.use(cors()); // accept all requests
app.use(bodyParser.json({ type: '*/*' }));
router(app);
// npm install --save nodemon
// nodemon check for changes on the api files and restarts
// automaticaly
const nodePort = env.NODE_PORT || 3000;
const nodeIp = env.NODE_IP || 'localhost';
// Server Setup
const server = http.createServer(app);
server.listen(nodePort, nodeIp, function () {
console.log(`Application worker ${process.pid} started...`);
console.log(`IP: ${nodeIp} PORT: ${nodePort}`);
});