-
Notifications
You must be signed in to change notification settings - Fork 2
HTTP Calls in Node.JS
This document explains the procedure to read content from a web address using the HTTP object in Node.JS.
We incljude previous code for setting up a very simple web server implementation in the Galileo Board using Node.js. and controlling one of the GPIO pins (#9).
This exercise is based on a more extent explanation of Node.JS by Nicolas Vailliet here:
- Intel Software Academic Program
- This is also based in node.js documentation.
This WIKI page is a living document that may be changed at any moment without prior notice.
All procedures and instructions shared in this GIT Hub Account are given to you "as-is" without any implicit or explicit warranty of success.
To the extent permitted by law, I accept no liability or responsibility for any damage or loss you may get into while following this procedure.
As of the time this post was written, I am affiliated to Intel Corporation as a regular employee based in Costa Rica, working as an Applications Developer.
All content in this page is provided voluntarily and personally independently of Intel.
You need:
- Perform the Boot from SD Card procedure (Optional)
- Perform the Connecting through LAN procedure
- You can use Connecting through LAN procedure
- example for May 20, 12:57pm:
date 0520125714
This will make node.js run better.
- Using the puTTY terminal, create a folder to store your server code. i.e.: /home/root/tinyWebServer
cd /home/root
mkdir tinyWebServer
- Access the folder you just created
cd /home/root/tinyWebServer
- Install galileo-io npm package
npm install galileo-io
- Run WinSCP and connect to the Galileo Board via Ethernet
- Look for the folder you just created. I.e.: /home/root/tinyWebServer
- Create a new file named 'server.js' with the following code inside:
var http = require('http');
var Galileo = require("galileo-io");
var board = new Galileo();
var delayTime = 1000;
var targetPin = 9;
var intervalHandler;
var byte = 0;
var intervalFunction = function() {
console.log("Interval Reached - " + byte + " - " + delayTime);
board.digitalWrite(targetPin, (byte ^= 1));
};
function ajaxCall() {
console.log("ajaxCall() reached");
var options = {
hostname: 'spicr.net',
port: 80,
path: '/iotest.json',
method: 'GET'
};
console.log(options);
var req = http.request(options, function(res) {
console.log("GOOD: http.request function");
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log("res.on function reached");
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
console.log("ajaxCall() ended");
}
board.on("ready", function() {
ajaxCall();
console.log("Ready Function Reached!");
this.pinMode(targetPin, this.MODES.OUTPUT);
intervalHandler = setInterval(intervalFunction, delayTime);
});
http.createServer(function(req, res) {
console.log("Request Received!");
var url = req.url;
clearInterval(intervalHandler);
console.log("Current Delay Time: " + delayTime);
if (delayTime > 200)
delayTime = delayTime - 200;
else
delayTime = 1000;
console.log("New Delay Time:" + delayTime);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n' + url);
intervalHandler = setInterval(intervalFunction, delayTime);
}).listen(1337, '169.254.1.1');
console.log('Server running at http://169.254.1.1:1337');
- run the file in node.js by executing
node server.js
- You'll see the Call/Response interaction in the terminal after a few seconds.
- Using your web browser in your computer, access
http://169.254.1.1:1337
- The page will returno a 'Hello World' string to the browser.
- The system will blink a signal in pin 9 that will vary its frequency every time the web server is accessed from the browser. It will also show different debug information in the console.