New to NodeJS and JavaScript in general; how do I do basic file I/O?
<!doctype html>
<html lang="en">
<head></head>
<body>
<a href="//foo">bar</a>
</body>
</html>
// Here is how I would do it if this was in <head></head>
document.open();
document.write('<a href="/bar">Out with the old -');
document.write('<a href="/new_bar">in</a> with the new!</a>');
var all_href = [], l = document.links;
document.close();
for (var i = 0; i < l.length; i++) {
all_href.push(l[i].href);
}
// `all_href` should contain: ["http://{host}/bar", "http://{host}/new_bar"]
So you want to manipulate DOM serverside before sending it to the client? If so, take a look at this (using "jsdom"): http://marksoper.me/Server-side-DOM-manipulation-in-Nodejs-with-JSDOM-JQuery-and-Mustache-Templates-April-25-2011.html
@MattiasHognas didn't accept my edit, so going off his link this is what I am thinking was meant:
require.paths.unshift('/usr/local/lib/node_modules');
var fs = require('fs'),
jsdom = require('jsdom');
var read_and_edit_me = fs.readFileSync('read_and_edit_me.html','utf-8');
var document = jsdom.jsdom(read_and_edit_me);
Then document
should be able to used as in my question's snippet?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.