-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
113 lines (88 loc) · 3.1 KB
/
app.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict';
/**
* Module dependencies.
*/
const keyPublishable = 'pk_test_sZay0UdHi8gZBfIRtvWefcLy';
const keySecret = 'sk_test_ta2435vzjD2vjo0eIP9gMPQk';
const stripe = require("stripe")(keySecret);
const bodyParser = require("body-parser");
var express = require('express');
const app = express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
var cluster = require('cluster');
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var config = require('./config/config');
var db = require('./config/sequelize');
var passport = require('./config/passport');
var winston = require('./config/winston');
var port = process.env.PORT || config.port;
var cors = require('cors');
var allowCrossDomain = function(req, res, next) {
if ('OPTIONS' === req.method) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.send(200);
}
else {
next();
}
};
app.use(cors());
app.use(allowCrossDomain);
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
if(env === 'development'){
return createApp();
}
// Code to run if we're in the master process
if (cluster.isMaster) {
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
// Listen for dying workers
cluster.on('exit', function (worker) {
// Replace the dead worker, we're not sentimental
console.log('Worker ' + worker.id + ' died :(');
cluster.fork();
});
// As workers come up.
cluster.on('listening', function(worker, address) {
console.log('A worker with #'+worker.id+' is now connected to ' + address.address + ':' + address.port);
});
// Count requestes
Object.keys(cluster.workers).forEach(function(id) {
cluster.workers[id].on('message', function(){
console.log('message');
});
});
// Code to run if we're in a worker process
} else {
createApp();
}
function createApp(){
//Initialize database models
db.init(function(){
//Initialize Express
require('./config/express')(app, passport, db);
app.listen(port);
winston.info('Express app started on port ' + port);
});
}