Skip to content

Commit

Permalink
updated server
Browse files Browse the repository at this point in the history
  • Loading branch information
jonniespratley committed Dec 19, 2014
1 parent a142594 commit 7f8b3f1
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 156 deletions.
5 changes: 5 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
"security":{
"salt":"angular-cms"
},
"https":{
"key": "./config/apache.key",
"cert":"./config/apache.crt"
},

"db":{
"name":"angular-cms",
"username":"amadmin",
Expand Down
74 changes: 74 additions & 0 deletions routes/cms-proxy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,77 @@
var fs = require('fs'),
util = require('util'),
httpProxy = require('http-proxy');


module.exports = function (config, app) {
console.warn( 'cms-proxy', 'initialized' );

/**
* @TODO - HTTPS Key and Cert
*
* This is the location of your https cert and key.
*/
var httpsKey = fs.readFileSync(config.https.key).toString();
var httpsCert = fs.readFileSync(config.https.cert).toString();


/**
* @TODO - Proxy Options
* This object holds options used for creating a proxy server.
*/
var options = {
port: null,
host: {
hostname: 'localhost',
port: 8181
},
proxy: {
hostname: 'localhost',
port: 5001
},
api: {
hostname: 'localhost',
port: 5151
},
key: httpsKey,
cert: httpsCert,
hostncmsOnly: true,
router: {}
};

//Create proxy server and proxy requests
var proxyServer = httpProxy.createServer(options, function(req, res, proxy) {

console.log('Proxy server started on port: ' + options.host.port);

// console.log('proxyServer', options);
if (req.url.match(/^\/api\//)) {
proxy.proxyRequest(req, res, {
host: '127.0.0.1',
port: options.api.port
});
util.log('Routing request: API server'.warn);
}
else if (req.url.match(/^\/1\//)) {

/* Default express server */
proxy.proxyRequest(req, res, {
host: 'api.parse.com'
});
util.log('Routing request: Parse Server'.warn);

} else {

/* Default express server */
proxy.proxyRequest(req, res, {
host: '127.0.0.1',
port: options.api.port
});
util.log('Routing request: App Server'.warn);
}
});


//Start the proxy server
proxyServer.listen(config.proxy.port);
};
67 changes: 0 additions & 67 deletions routes/cms-routes.js

This file was deleted.

3 changes: 3 additions & 0 deletions routes/cms-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var express = require('express'),
path = require('path'),
bodyParser = require( 'body-parser' );


module.exports = function (config, app) {
console.warn( 'cms-server initialized');

Expand All @@ -19,6 +20,8 @@ module.exports = function (config, app) {
}
};



router.use(express.static(config.staticDir, options));
app.all('/', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
Expand Down
95 changes: 6 additions & 89 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,10 @@
* Server - This is the Node.js Server.
* @object
*/
var fs = require('fs'),
util = require('util'),
http = require('http'),
express = require('express'),
httpProxy = require('http-proxy'),
app = express();


/**
* @TODO - Externalize configuration for server and proxy, mongodb
*/
var express = require('express');
var fs = require('fs');
var cmsRouter = require('./routes/cms-router.js');
var app = express();
var config = JSON.parse(fs.readFileSync('./config/config.json'));


/**
* @TODO - HTTPS Key and Cert
*
* This is the location of your https cert and key.
*/
var httpsKey = fs.readFileSync('./config/apache.key').toString();
var httpsCert = fs.readFileSync('./config/apache.crt').toString();

/**
* @TODO - Proxy Options
* This object holds options used for creating a proxy server.
*/
var options = {
port: null,
host: {
hostname: 'localhost',
port: 8181
},
proxy: {
hostname: 'localhost',
port: 5001
},
api: {
hostname: 'localhost',
port: 5151
},
key: httpsKey,
cert: httpsCert,
hostncmsOnly: true,
router: {}
};




var cmsRoutes = require('./routes/cms-routes');
cmsRoutes.mount(config, app);




//Create proxy server and proxy requests
var proxyServer = httpProxy.createServer(options, function(req, res, proxy) {

console.log('Proxy server started on port: ' + options.host.port);

// console.log('proxyServer', options);
if (req.url.match(/^\/api\//)) {
proxy.proxyRequest(req, res, {
host: '127.0.0.1',
port: options.api.port
});
util.log('Routing request: API server'.warn);
}
else if (req.url.match(/^\/1\//)) {

/* Default express server */
proxy.proxyRequest(req, res, {
host: 'api.parse.com'
});
util.log('Routing request: Parse Server'.warn);

} else {

/* Default express server */
proxy.proxyRequest(req, res, {
host: '127.0.0.1',
port: options.api.port
});
util.log('Routing request: App Server'.warn);
}
});


//Start the proxy server
proxyServer.listen(process.env.PORT, process.env.IP);
var server = new cmsRouter.mount(config, app);
console.log(server);

0 comments on commit 7f8b3f1

Please sign in to comment.