EN
Node.js - document.write() equivalent
9 points
In this short article, we would like to show how to use document.write()
in NodeJS?
Node.js doesn't support document object that is implemented only in the browsers.
There is some quick solution to get the same effect:
xxxxxxxxxx
1
process.stdout.write('Something to print in console ...\n');
Example index.js
file:
xxxxxxxxxx
1
const document = {
2
write: text => {
3
process.stdout.write(text);
4
},
5
writeln: line => {
6
process.stdout.write(`${line}\n`);
7
}
8
};
9
10
11
// Usage example:
12
13
document.writeln('Something to print in console ...');
14
document.writeln('Something to print in console ...');
15
document.writeln('Something to print in console ...');
16
17
document.write('Something to print in console ...');
18
document.write('Something to print in console ...');
19
document.write('Something to print in console ...');
Running with:
xxxxxxxxxx
1
node ./index.js
Output:
xxxxxxxxxx
1
Something to print in console ...
2
Something to print in console ...
3
Something to print in console ...
4
Something to print in console ...Something to print in console ...Something to print in console ...