Created
July 24, 2015 23:06
-
-
Save pgarciacamou/82e49e1d11c4310093dd to your computer and use it in GitHub Desktop.
Mini node server REST API with express that prints to the console through web. Created to replace jsconsole.com and to debug phones that are not iphones or compatible.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This console connects to a server and sends data to be printed. | |
* | |
* @note Server must allow CORS requests. | |
* | |
* @param options {Object} Set of options to make the ajax requests. | |
* | |
* @example | |
* var c = new Console({ | |
* localhost: "10.0.1.8", | |
* route: "array" | |
* }); | |
*/ | |
function Console(options){ | |
options = options || {}; | |
this.method = (options.method || "post").toUpperCase(); | |
this.port = options.port || 3000; | |
this.route = "/" + (options.route || ""); | |
this.localhost = options.localhost || "localhost"; | |
this.dataType = options.dataType || "json"; | |
this.contentType = options.contentType || "application/json"; | |
} | |
Console.prototype.log = function() { | |
var json = { | |
data: [].slice.call(arguments, 0) | |
}; | |
var httpRequest = new XMLHttpRequest(); | |
httpRequest.open(this.method, "//" + this.localhost + ":" + this.port + this.route, true); | |
httpRequest.setRequestHeader("Content-Type", "application/json"); | |
httpRequest.send(JSON.stringify(json)); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "express-console", | |
"version": "1.0.0", | |
"description": "Grunt server with express that prints to the console", | |
"main": "index.js", | |
"dependencies": { | |
"body-parser": "^1.13.2", | |
"express": "^4.13.1" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "https://github.com/pgarciacamou/express-console.git" | |
}, | |
"author": "", | |
"license": "ISC", | |
"bugs": { | |
"url": "https://github.com/pgarciacamou/express-console/issues" | |
}, | |
"homepage": "https://github.com/pgarciacamou/express-console" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var app = express(); | |
var bodyParser = require('body-parser'); | |
app.use(function(req, res, next) { | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
next(); | |
}); | |
app.use(bodyParser.json()); | |
app.post('/', function(req, res){ | |
if(!req.body) res.sendStatus(400); | |
req.body.data.forEach(function(d){ | |
console.log(d); | |
}); | |
res.sendStatus(200); | |
}); | |
app.post('/object', function(req, res){ | |
if(!req.body) res.sendStatus(400); | |
console.log(req.body); | |
res.sendStatus(200); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment