Having trouble with basic node.js server

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);

I would recommend you get and use express, then you could do routes something like this:

err my mistake . …

Thanks William!

I loaded express and was able to get the basic web server working. Much simpler code as well.

Regards,
Gary

var express = require(‘express’);
var app = express();

app.get(’/’, function(req, res){
res.send(‘Hello World’);
});

app.get(’/socket.html’, function(req, res){
res.sendfile(’/var/lib/cloud9/socket.html’);
});

var server = app.listen(8000, function() {
console.log(‘Listening on port %d’, server.address().port);
});

Great!

Yesterday I mentioned MEAN, but that really isnt possible on the BBB yet. I did some research, and apparently MongoDB still doesnt work well on ARM. It’s been a year since i last checked . . . Their excuse is that memory mapped “documents” wont work well on embedded devices, which is of course a load of bull. Matter of fact it makes more sense on an embedded device than anything else.

The good news is that Angularjs Express, and socket.io will all work fine to achieve the same goal.

Great!

Yesterday I mentioned MEAN, but that really isnt possible on the BBB yet. I did some research, and apparently MongoDB still doesnt work well on ARM. It’s been a year since i last checked . . . Their excuse is that memory mapped “documents” wont work well on embedded devices, which is of course a load of bull. Matter of fact it makes more sense on an embedded device than anything else.

Use Redis nosql database which is lightning fast and scales really well.

Regards,
John

John, I do not know if it’s needed, necessary or most efficient for my own project. Basically I’m writing ( in my spare time ) a client side alarm / mp3 player app that gets served music files through Node.js to the client.

This includes but is not limited to a playlist or listing of music available on the server. Methods to play, randomize, loop through, or otherwise order music. A clock feature with an alarm clock, that has the ability to set an alarm by a set amount of time, or set a specific time to go off. And whatever else I can dream up.

For me right now on this project it seems that socket.io makes the most sense. I can iterate through a directory, and feed the client a list of files, semi dynamically provide links, and tags to the client. Then the client can provide additional grunt work by getting file stats through using the html5 audio tag( using javascript ). This keeps me from having to install additional libs / tools like ffmpeg on the server side while keeping things as simple( and clear ) as possible.

I am also reading up on Mosquitto / MQTT for this( less so for this but a consideration ), and other purposes.

Anyway, my only real goal is to learn. Learning about new technology( to me ) and using it is part of that.

Forgot to mention that I have been doing a lot of reading on the concept of MEAN stacks lately, and the more I read, the more I think that MEAN is overly cumbersome / complex for a task this simple. I can see it working well for a large CMS like app,but for small ish projects I feel it is far too complex.