I’m probably missing something very simple, but I’ve struggled with this most of the day and searched for a solution without success.
Ultimately, I want to get a temperature from several sensors and push to a gauge on a webpage using socket.io in real time. However, I can’t seem to get a basic html file to load properly, so I’ve tried to strip it down to the basics and debug the issue.
Running a beaglebone black that I just received this week. rev 00A5 S/N 5000BBBK3000 running BoneScript 0.2.4 at 10.0.0.24
Node is version 1.3.10 as far as I can tell.
I’ve been learning node, javascript and angular.js for almost a year, but am still very much a noob.
Code is in a file called server.js which loads an html file called socket.html. If I put the html in a string variable and write it out, it works perfectly. If I call fs.readFile and then write the data returned, it displays a blank browser page.
Have tried converting the buffer to a string, changing the content type, and setting content length.
The only difference I can see is when I hit the ‘stringvar’ route and the html output works, in WireShark I see the server return a “line based text data” at the end of the packet which contains the HTML. When taking the “socket.html” route, I see the same ‘chunk data 0 octets’ message, but no “line based text data” at the end.
Any assistance would be greatly appreciated. -gary
server.js
var http = require(“http”);
var url = require(‘url’);
var fs = require(‘fs’);
var server = http.createServer(function(request, response){
console.log(‘Connection’);
var path = url.parse(request.url).pathname;
switch(path){
case ‘/’:
response.writeHead(200, {‘Content-Type’: ‘text/html’});
response.write(__dirname + path);
response.end();
break;
case ‘/socket.html’:
fs.readFile(__dirname + path,‘utf-8’,function (error, data){
if (error) {
throw error;
}
console.log(data);
response.writeHead(200, {‘Content-Type’: ‘text/html’});
response.write(data,‘utf-8’);
response.end();
});
break;
case ‘/stringvar’:
var mydata = ‘<!doctype html>
This is our html string
’;console.log(mydata);
response.writeHead(200, {‘Content-Type’: ‘text/html’});
response.write(mydata, ‘utf-8’);
response.end();
break;
default:
response.writeHead(404);
response.write(“default path: oops this doesn’t exist - 404”);
break;
}
response.end();
});
server.listen(8000);