Skip to content

Commit

Permalink
Add debugging help to API & server files.
Browse files Browse the repository at this point in the history
  • Loading branch information
akavlie committed Apr 4, 2012
1 parent a910f3a commit 6d1be24
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
19 changes: 10 additions & 9 deletions lib/nodester-api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var http = require('http'),
encode = require("./encoding"),
HOST = "api.nodester.com",
PORT = 80;
PORT = 80,
debug = process.env.NODE_ENV === 'debug';
// Generate Query parameters from params(json)
function generateQueryParams(params) {
console.log('generating params');
Expand Down Expand Up @@ -39,11 +40,11 @@ function request(method, path, data, credentials, callback) {
var queryString = generateQueryParams(data);
var formattedPath = "/" + path + ((method == "GET" && queryString.length > 0) ? "?" + queryString : "");

// if (method == 'DELETE' && formattedPath == '/app') {
// formattedPath = '/app/' + data.appname;
// queryString = '';
// data = '';
// }
if (method == 'DELETE' && formattedPath == '/apps') {
formattedPath = '/app/' + data.appname;
queryString = '';
data = '';
}
if (method == 'DELETE' && formattedPath == '/appdomains') {
formattedPath = '/appdomains/' + data.appname + '/' + data.domain;
queryString = '';
Expand All @@ -58,7 +59,7 @@ function request(method, path, data, credentials, callback) {
}


if (true || process.env["debug"]) {
if (true || debug) {
console.log("formatted path ===> ", formattedPath);
console.log("method ===> ", method);
console.log("params ===> ", data);
Expand All @@ -79,7 +80,7 @@ function request(method, path, data, credentials, callback) {
options.headers["Content-Length"] = queryString.length.toString();
options.headers["Content-Type"] = "application/x-www-form-urlencoded";
}
if(process.env["debug"]){
if (debug) {
console.log("---Options---");
console.log('HOST ===> ' + options.host);
console.log('PORT ===> ' + options.port);
Expand All @@ -93,7 +94,7 @@ function request(method, path, data, credentials, callback) {
if (method != "GET") req.write(queryString);

req.on("response", function(res) {
if(process.env["debug"]){
if (debug) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
}
Expand Down
13 changes: 6 additions & 7 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,19 @@ app.get('/register', checkAuth, function(req, res) {



// Need to write paths to all ndoester APIs
// including GET and POST - done
// Need to figure out REGEX - done
// Forward requests to Nodester API
app.all("/api/*", checkAuth, function(req, res, next) {
var debug = process.env.NODE_ENV === 'debug';
var params = "";
if(process.env["debug"]){
if (debug) {
console.log('a request of verb ' + req.method);
console.log('request params ' + req.params);
if (req.method === 'DELETE') return;
}
// based on verb, get params
// based on HTTP verb, get params
if (req.is_logged === true) {
if (req.method == "GET") {
params = req.query;

} else {
params = req.body;
}
Expand All @@ -171,7 +170,7 @@ app.all("/api/*", checkAuth, function(req, res, next) {
nodester.request(req.method, req.params[0], params, req.user.creds, function(response) {
res.send(response);
});
} else if(req.method==='POST' && req.params[0]==='user'){
} else if (req.method==='POST' && req.params[0] === 'user') {
params = req.body;
res.header('Content-Type', 'application/json');
// method, api path, data, credentials, callback
Expand Down

0 comments on commit 6d1be24

Please sign in to comment.