Intel® Makers
Intel® Edison, Intel® Joule™, Intel® Curie™, Intel® Galileo
Announcements
Welcome - This is a Peer-to-Peer Forum only. Intel has discontinued these products but you may find support from other customers on this Forum
9881 Discussions

Serving Up and HTML 'File' with the Edison

KRebe
Novice
1,062 Views

Hi, all!

I got the Intel Edison Sparkfun Inventors Kit from Sparkfun. I'm doinghttps://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-for-edison-experiment-guide/experiment-5... tutorial number 5 and the tutorial has the Edison serve up a web page via socket.io like so:

var server = http.createServer(function(req, res) { 

res.writeHead(200);

 

res.write(" <</span>!DOCTYPE html> \

 

<</span>html> \

 

<</span>head> \

 

<</span>title>LED Controller<</span>/title> \

 

<</span>script src='/socket.io/socket.io.js'><</span>/script> \

 

<</span>/head> \

 

<</span>body> \

 

<</span>p><</span>button onclick='toggle()'>TOGGLE<</span>/button><</span>/p> \

 

<</span>script> \

 

var socket = io.connect('http:// http://" +

 

req.socket.address().address + ":" +

 

port + "'); \

 

function toggle() { \

 

socket.emit('toggle'); \

 

} \

 

<</span>/script> \

 

<</span>/body> \

 

<</span>/html>");

 

res.end();

 

});

This is silly to me. I'm literally printing out HTML inside of Javascript. Then I'm printing out Javascript inside of printed HTML inside of Javascript.

I'm just wondering if I can code the html into a separate file and then serve up that file instead of doing it like this.

Thanks! - Runningman.

1 Solution
Diego_V_Intel
Employee
155 Views

Hi KeenanRebera,

The res.write() method receives a string argument which is the HTML part. I'd say that you could have a separate HTML file and then, read the content of the file and pass it as a long string to the res.write() method. Anyhow, I'm not familiar with the tutorial so it might be better if you ask directly in the SparkFun forum.

On the other hand, you might want to check the HTTP API documentation in the following site for additional details: https://nodejs.org/api/http.html HTTP Node.js v5.7.0 Manual & Documentation

Regards,

Diego

View solution in original post

1 Reply
Diego_V_Intel
Employee
156 Views

Hi KeenanRebera,

The res.write() method receives a string argument which is the HTML part. I'd say that you could have a separate HTML file and then, read the content of the file and pass it as a long string to the res.write() method. Anyhow, I'm not familiar with the tutorial so it might be better if you ask directly in the SparkFun forum.

On the other hand, you might want to check the HTTP API documentation in the following site for additional details: https://nodejs.org/api/http.html HTTP Node.js v5.7.0 Manual & Documentation

Regards,

Diego

Reply