Skip to content

Instantly share code, notes, and snippets.

@basiclines
Last active December 10, 2015 17:18
Show Gist options
  • Save basiclines/4466461 to your computer and use it in GitHub Desktop.
Save basiclines/4466461 to your computer and use it in GitHub Desktop.
NodeJs API server that returns the content of the url
// CURL
var util = require('util');
var exec = require('child_process').exec;
// API RESPONSE
var http = require('http');
var sys = require('sys');
var server = http.createServer(function(req, res) {
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
'Access-Control-Allow-Headers': 'Content-Type'
});
getContent(req.url, res);
});
server.listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
function getContent(location, response) {
var base_path = "https://my.website.com"
var command = 'curl -sL'+base_path+location;
child = exec(command, function(error, stdout, stderr){
if(error !== null) {
console.log('exec error: ' + error);
}
var data = {
"url":base_path+location,
"content": stdout
}
response.end(JSON.stringify(data));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment