-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicExample.mjs
37 lines (30 loc) · 1.06 KB
/
basicExample.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import http from 'http';
import { readFileSync, createReadStream } from 'fs';
import net from 'net';
// Using basic std
const stdin = process.stdin.on('data', (msg) =>
console.log('entrada terminal', msg.toString()),
);
const stdout = process.stdout.on('data', (msg) =>
console.log('saida terminal', msg.toString()),
);
stdin
.pipe(stdout)
.on('error')
.on('end')
.on('close');
// Using http server, run "node -e "process.stdout.write(crypto.randomBytes(1e9))" > big.file" to create 100000 bytes into big.file
http
.createServer((req, res) => {
// Using readFileSync
// The code below'll not compile because the file'll be too big
const file = readFileSync('big.file').toString();
res.write(file);
res.end;
// Using createReadStream
// Separates file into smaller chunks
createReadStream('big.file').pipe(res);
})
.listen(3000, () => console.log('Server running at port 3000'));
// Using socket, run "node -e "process.stdin.pipe(require('net').connect(1338))""
net.createServer((socket) => socket.pipe(process.stdout)).listen(1338);