If you are looking to server static content, then you don't need the espruino todo the heavy lifting, you can leave that to the much more powerful browser.
On the sd card, store .gz versions of the files, and then have the onPageRequest handler server the compressed content:
function onPageRequest(req, res) {
var a = url.parse(req.url, true);
console.log(a);
file=a.pathname;
if (file == '/') {
file='/index.htm';
}
var headers= {};
var f;
try {
f = E.openFile('www' + file, "r");
}
catch(e) {
console.log('no file');
// look for compressed
try {
f = E.openFile('www' + file + '.gz', "r");
headers['Content-Encoding']='gzip';
} catch(e) {}
}
if (f !== undefined) {
mime='text/html';
if ( file.substr(-2) == 'js' ) mime='application/javascript';
if ( file.substr(-3) == 'css' ) mime='text/css';
if ( file.substr(-3) == 'ico' ) mime='image/vnd.microsoft.icon';
headers['Content-Type']= mime;
res.writeHead(200, headers);
console.log('started ' + file );
f.pipe(res,{chunkSize:512, end:false, complete:function(){
console.log("Complete");
f.close();
res.end();
} } );
} else {
res.writeHead(404, {
'Content-Type': 'text/plain'
});
res.end("404: Page " + a.pathname + " not found");
}
}
This assumes all of the web assets are in www ( you could use sub folders www/css)
As an example of fetching: http://192.168.15.13/pure-min.css
The difference is:
pure-min.css 31KB 2 secs to fetch
pure-min.css.gz 600ms secs to fetch
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
If you are looking to server static content, then you don't need the espruino todo the heavy lifting, you can leave that to the much more powerful browser.
On the sd card, store .gz versions of the files, and then have the onPageRequest handler server the compressed content:
This assumes all of the web assets are in
www
( you could use sub folders www/css)As an example of fetching:
http://192.168.15.13/pure-min.css
The difference is:
pure-min.css 31KB 2 secs to fetch
pure-min.css.gz 600ms secs to fetch