//Lets require/import the HTTP module
var http = require('http');
//Lets define a port we want to listen to
const PORT=8080;
//We need a function which handles requests and send response
function handleRequest(request, response){
//response.end('It Works!! Path Hit: ' + request.url);
var WooCommerceAPI = require('woocommerce-api');
// Initialize the WooCommerceAPI class
var WooCommerce = new WooCommerceAPI({
});
// GET example
WooCommerce.get('products', function (err, data, res) {
//console.log(res);
//var fs = require('fs');
//var jsonContent = JSON.parse(JSON.stringify(res, null, 4))
var jsonContent = JSON.parse(res)
for (var i = 0; i < jsonContent["products"].length; i++)
{
var name = jsonContent["products"][i];
//This works in console
//console.log(name['title']);
//console.log(name['id']);
//console.log(name['sku']);
//console.log(name['regular_price']);
response.writeHead(200, {'Content-Type': 'text/plain' });
response('It Works!! ' + name['id'] + ' ' + name['title']);
//response.end('It Works!! Path Hit: ' + name['id'] + ' ' + name['title']);
}
//response.end('It Works!! Path Hit: ' + name['id']);
});
//response.end('It Works!! Path Hit: ' + name['id']);
}
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!