Community
cancel
Showing results for 
Search instead for 
Did you mean: 
KRebe
Novice
984 Views

Serving Up and HTML 'File' with the Edison

Jump to solution

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.

Tags (1)
1 Solution
Diego_V_Intel
Employee
77 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
78 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

Reply