Was ich tue ist fs, um 5 Stücke von HTML-Seite Teile (html, head, chead, topaside, main, footer
) zusammen zu sticken. Der Dateiname lautet htmlpage.js
, Sie können also einfach node htmlpage.js file1 file2 file3 ...
im Befehlszeilentool ausführen, und diese HTML-Page-Teile zusammenheften, dann spucken Sie file1.html, file2.html, file3.html ...
aus. Ich mag es nicht, Vorlagen-Engine/Bibliothek/Framework oder was auch immer zu verwenden, besonders wenn ich lerne.
Hier ist der sorce Code:Node.js wie mit async Callback zu iterieren?
'use strict';
const fs = require('fs'),
head = fs.createReadStream('./html-parts/head.html', 'utf8'),
topaside = fs.createReadStream('./html-parts/topaside.html', 'utf8'),
footer = fs.createReadStream('./html-parts/footer.html', 'utf8');
let name = process.argv.slice(2),
htmlray = [],
ni = 0,
nl = name.length;
for (ni; ni < nl; ni ++) {
let cheadP = './html-parts/' + name[ni] + '-head.html',
mainP = './html-parts/' + name[ni] + '-main.html',
htmlP = name[ni] + '.html',
chead = fs.createReadStream(cheadP, 'utf8'),
main = fs.createReadStream(mainP, 'utf8'),
html = fs.createWriteStream(htmlP, 'utf8');
//let those parts form an array
htmlray = [html, head, chead, topaside, main, footer];
openendPipe(htmlray[1], htmlray[0]);
htmlray[1].on('end',() => {
openendPipe(htmlray[2], htmlray[0]);
htmlray[2].on('end',() => {
openendPipe(htmlray[3], htmlray[0]);
htmlray[3].on('end',() => {
openendPipe(htmlray[4], htmlray[0]);
htmlray[4].on('end',() => {
htmlray[5].pipe(htmlray[0]);
htmlray[5].on('end',() => {
console.log(name + '.html' + ' created');
});
});
});
});
});
}
function openendPipe(src, dst) {
return src.pipe(dst, {end: false});
}
Was aber, wenn die htmlray
100 Teile hat, ich will es sich pipeblock
nennen können, eine Iteration zu tun, diese Codes zu ersetzen, lassen:
openendPipe(htmlray[1], htmlray[0]);
htmlray[1].on('end',() => {
openendPipe(htmlray[2], htmlray[0]);
htmlray[2].on('end',() => {
openendPipe(htmlray[3], htmlray[0]);
htmlray[3].on('end',() => {
openendPipe(htmlray[4], htmlray[0]);
htmlray[4].on('end',() => {
htmlray[5].pipe(htmlray[0]);
htmlray[5].on('end',() => {
console.log(name + '.html' + ' created');
});
});
});
});
});
habe ich versucht, diese Lösungen, die sie nicht arbeiten:
Lösung 1:
(function() {
let i = 0, count = 1;
function nextpipe() {
let arr = arguments[0];
i ++;
if (count > 5) return;
openendPipe(arr[i], arr[0]);
count ++;
arr[i].on('end', nextpipe);
}
return nextpipe;
})();
//then replace 'pipeblock' with 'nextpipe(htmlray)';
//console.log: nextpipe is undefined.
Lösung:
//replace 'pipeblock' with these code
let pi = 1,
pl = htmlray.length - 1;
htmlray[pi].pipe(htmlray[0], {end: false});
htmlray[pi].on('end', nextpipe);
function nextpipe() {
if (pi > pl) return console.log(name + '.html' + ' created');;
pi ++;
htmlray[pi].pipe(htmlray[0], {end: false});
htmlray[pi].on('end', nextpipe);
}
//cosole.log:
//htmlray[pi].pipe(htmlray[0], {end: false});
//TypeError: Cannot read property 'pipe' of undefined
Ja, ich habe einige Videos geschaut und von Zeit zu Zeit die Dokumentation über Versprechen und Generator gelesen, aber ich habe sie jedes Mal entlassen. Nun, wenn sie die einzige Lösung, aber keine Iteration sind, muss ich sie ausprobieren. Danke für die Erinnerung. – alexcres
Ich selbst habe vor ein oder zwei Jahren viel Zeit verloren, um dieses Zeug zu überspringen (und ich bereue es wirklich) und versuche, mit Callback Loops, asynchronen Wasserfällen usw. zu experimentieren - Versprechen sind elegant und kraftvoll. Und schon mit der Fehlerbehandlung (das sollte auch wichtig sein) - füge einfach '.catch()' zu deinem endgültigen Versprechen hinzu. –
Waite, ich erinnere mich nur, dass für meine Frage von async Callback 100 Mal, in Aussicht, Sie müssen dann() für den nächsten Anruf 100 Mal verwenden. Und im Generator müssen Sie immer noch 100 Mal nachgeben. Habe ich recht? Ich will das nicht wirklich machen. – alexcres