четверг, 29 июня 2017 г.

Шпаргалка по Node.js: HTTP Server, Socket, TLS, UDP, WebSocket, Path, Files, Stream, Spawn, MySQL

http для браузера
socket для telnet
udp для передачи видео
tls для защищенных соединений

// Server and Client

var fs = require('fs');

var writeStream = fs.createWriteStream('client-logs.txt');

var http = require('http');

var server = http.createServer();

server.on('request', function (request, response) {
    console.log(request.method);
    console.log(request.url);
    console.log(request.headers);
    request.on('data', function (data) {
        writeStream.write(data);
    });
    request.on('end', function () {
        writeStream.end();
    });
    response.setHeader('Content-Length', '1000');
    response.removeHeader('Max-Length', '10');
    response.writeHead(200, 'OK', {'Content-Type': 'text/html'});
    response.write('Hi');
    response.end('Bye');
    server.close();
});

server.listen(80, '127.0.0.1', function (error) {
    if (error) {throw error;}
    console.log('Server started at 127.0.0.1:80');
});

var options = {
    host: "www.google.com",
    port: 80,
    path: "/upload",
    method: "POST",
    agent: new Agent({
        maxSockets: 10
    })
};

var request = http.request(options);

request.on('response', function (response) {
    console.log('STATUS:', response.statusCode);
    console.log('HEADERS:', response.headers);
    console.log(response.httpVersion);
    response.setEncoding('utf8');
    response.on('data', function (chunk) {console.log(chunk);});
    response.on('end', function (chunk) {console.log('END');});
});

request.on('socket', function (socket) {
    socket.emit('agentRemove');
});

request.write('data');
request.end();

// Transmission control protocol TCP Server - Use with command line Telnet

// socket умеет и читать данные и писать данные,
// то есть и принимать их on('data'), on('end')
// и отсылать write(data), end(data)

require('net').createServer(function (socket) { // new connection
    socket.on('data', function(data) {
        // got data
    });
    socket.on('end', function(data) {
        // connection closed
    });
    socket.write('Some string');
}).listen(4001);

// telnet 127.0.0.1 4001

var server = require('net').createServer();

var connections = []; // Request connections

server.on('connection', function (socket) {

    connections.push(socket);

    socket.setEncoding('utf8');

    socket.on('data', function(data) {
        // got data
        console.log('got:', data.toString());

        connections.forEach(function(otherSocket) {
            if (otherSocket !== socket) {
                otherSocket.write(data);
            }
        });

        if (data.trim().toLowerCase() === 'quit') {
            socket.write('Bye bye!');
            socket.end();
        } else {
            socket.write(data);
        }
    });

    socket.on('end', function(data) {
        // connection closed
        console.log('Client connection ended');
    });

    socket.on('close', function() {
        console.log('connection closed');
        var index = connections.indexOf(socket);
        connections.splice(index, 1); // remove closed socket connections
    });

    socket.on('error', function(error) {
        console.log(error);
    });

    socket.pause();
    socket.resume();

    socket.write('Some string');
    socket.end('Bye');
});

// socket.setKeepAlive(true) periodically sends an empty packet to keep the connection alive
// whereas socket.setTimeout() is used to define a local inactivity timeout

server.setKeepAlive(true, 10000); // keep the connection alive on both peers, delay 10 seconds before next send

server.setTimeout(60000); // servert timeout 1 minute
server.on('timeout', function() {
    server.write('idle timeout, disconnecting, bye!');
    server.end();
});

// socket.setTimeout(60000, function() { // альтернативная запись таймаута
//     socket.end('idle timeout, disconnecting, bye!');
// });

server.setNoDelay(true); // force data to be sent immediately after each write command socket.write()

server.on('error', function (error) {
    throw error;
});

server.on('close', function () {
    console.log('Server is now closed');
});

server.on('listening', function () {
    console.log('Server started at 127.0.0.1:4001');
});

server.listen(4001, '127.0.0.1');

setTimeout(function () {
    server.close();
}, 5000);


// telnet localhost 4001

var ws = require('fs').createWriteStream('mysocketdump.txt');
require('net').createServer(function(socket) {
    socket.pipe(ws); // Write data from telnet into file
}).listen(4001);

// telnet localhost 4001

require('net').createServer(function(socket) {
    var rs = require('fs').createReadStream('mysocketdump.txt');
    rs.pipe(socket); // Send text from file to telnet
}).listen(4001);

// Transmission control protocol TCP Client

var net = require('net');

var host = "www.google.com";
var port = 4000;

var client = net.createConnection(port, host, function (response) {
    console.log('We have a new connection');
});

client.once('connect', function (response) {
    console.log('We have a new connection');
    response.setEncoding('base64');
    response.on('data', function (data) {
        console.log('some data has arrived:', data);
    });
});

client.on('error', function (error) {
    console.error('this error happened:' + error.message + ', code: ' + error.code);
});

client.on('close', function() {
    console.log('connection got closed, will try to reconnect');
});

client.write('here is a string for you!');
client.end();

// Datagram Server

// node udp_server_1.js
// echo hello | nc -u 0.0.0.0 4000
// This call sends a UDP packet with hello to the local host port 4000.
// Server output that’s something like the following:
// server got message: hello

var dgram = require('dgram');

var server = dgram.createSocket('udp4'); // UDP over IPv4

server.on('message', function (message, rinfo) { // rinfo - information about message sender (remote info)
    console.log('server got message: ' + message + ' from ' + rinfo.address + ':' + rinfo.port);
    server.send(message, 0, message.length, rinfo.port, rinfo.address, function() {
        // you can reuse the message buffer
    }); // Send message to message to a specifi c address and port.
});

server.on('listening', function() {
    var address = server.address();
    console.log('server listening on ' + address.address + ':' + address.port);
});

var port = 4000;

server.bind(port);

// Datagram Client

var dgram = require('dgram');

var client = dgram.createSocket('udp4'); // UDP over IPv4

client.on('message', function(message) {
    console.log('Got message back from server:', message.toString());
});

var port = 8000;

client.bind(port);

var message = new Buffer('this is a message');

client.send(message, 0, message.length, 4000, 'localhost', function(error, bytes) {
    if (error) {throw error;}
    client.close();
});

// Datagram Multicast Server

// Message multicasting can be useful when you don’t need to know the address of all peers. Peers just have to "tune in" and listen to that channel.
// For instance, you could have an event logging server listening on multiple "channels".
// When another process needs to log in to a "channel", it would multicast a message containing the event details into that "channel".
// Nodes can report their interest in listening to certain multicast channels by
// tuning in to that channel. In IP addressing, a space is reserved for multicast addresses.
// In IPv4 the range is between 224.0.0.0 and 239.255.255.255, but some of these are reserved.

// ./udp_client.js 230.1.2.3 4000

var server = require('dgram').createSocket('udp4');

server.on('message', function(message, rinfo) {
    console.log('server got message: ' + message + ' from ' + rinfo.address + ':' + rinfo.port);
});

server.bind(4000);

server.addMembership('230.1.2.3');

// Tells the kernel that this UDP socket should receive multicast messages for the multicast address 230.1.2.3.

// Sending Multicast Messages

var dgram = require('dgram');

var client = dgram.createSocket('udp4');

var message = new Buffer('this is a multicast message');

client.bind(4000);

client.setMulticastTTL(10); // Set the multicast time-to-live (TTL) to 10.

// This TTL tells the network how many hops (routers) the datagram can travel through before it is discarded.
// Every time a UDP packet travels through a hop, the TTL counter is decremented, and if 0 is reached, the packet is discarded.

client.send(message, 0, message.length, 4000, '230.1.2.3');

client.close();

// TLS SSL Server

var tls = require('tls');
var fs = require('fs');

var serverOptions = {
    key: fs.readFileSync('./my_key.pem'),
    cert: fs.readFileSync('./my_certificate.pem'),
    ca: [fs.readFileSync('fake_ca.pem')],
    requestCert: true, // asks the client to send over the certificate
    rejectUnauthorized: true // reject certificates that are not validated by the Certificate Authority chain
};

var server = tls.createServer(serverOptions);

server.on('secureConnection', function (clientStream) {
    console.log('got a new connection');
    console.log('client authorized:', clientStream.authorized);
    clientStream.on('data', function (data) {
        console.log('got some data from the client:', data);
        clientStream.write('Hey Hello Client!');
        if (data.toString().trim().toLowerCase() === 'quit') {
            clientStream.end('Bye bye!');
        }
    });
});

server.listen(4001);

// TLS SSL Client

var tls = require('tls');
var fs = require('fs');

var options = {
    key: fs.readFileSync('/path/to/my/private_key.pem'),
    cert: fs.readFileSync('/path/to/my/certificate.pem')
};

var client = tls.connect(4001, '127.0.0.1', options, function() {
    console.log('connected');
    console.log('authorized: ' + client.authorized);
    if (! client.authorized) {
        console.log('client denied access:', client.authorizationError);
    } else {
        // ...
    }
    client.on('data', function (data) {
        console.log('got some data from the server:', data);
        client.write('Hey, hello Server!');
        client.end('Bye bye!');
    });
});

// TLS chat server

var tls = require('tls');
var fs = require('fs');

var port = 4001;

var clients = [];

var options = {
    key: fs.readFileSync('server_key.pem'),
    cert: fs.readFileSync('server_cert.pem')
};

var server = tls.createServer(options, function(client) {
    clients.push(client);
    client.on('data', function(data) {
        var socket = client.socket;
        clients.forEach(function(clientFromArray) {
            if (clientFromArray !== client) {
                clientFromArray.write(socket.remoteAddress + ':' + socket.remotePort + ' said: ' + data);
            }
        });
    });
    client.on('close', function() {
        console.log('closed connection');
        clients.splice(clients.indexOf(client), 1);
    });
});

server.listen(port, function() {
    console.log('listening on port', server.address().port);
});

// HTTPS Server

var fs = require('fs');
var https = require('https');

var options = {
    key: fs.readFileSync('server_key.pem'),
    cert: fs.readFileSync('server_cert.pem'),
    requestCert: true, // asks the client to send over the certificate
    rejectUnauthorized: true // reject certificates that are not validated by the Certificate Authority chain
};

var server = https.createServer(options, function(request, response) {
    console.log('authorized:', request.socket.authorized);
    console.log('client certificate:', request.socket.getPeerCertificate());
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World!');
});

server.listen(4001, '192.168.1.100', function() {
    console.log('Server is listening now on port', server.address().port);
});

// HTTPS Client

var fs = require('fs');
var https = require('https');

var options = {
    host: '0.0.0.0',
    port: 4001,
    method: 'GET',
    path: '/'
};

var request = https.request(options, function (response) {
    console.log('response statusCode:', response.statusCode);
    console.log('server authorized:', response.socket.authorized);
    console.log('server certificate: ' + response.socket.getPeerCertificate());
    response.on('data', function (data) {
        console.log('got some data back from the server:', data);
    });
});

request.write('Hey!');
request.end();

// WebSocket Server

var ioserver = require('socket.io')

ioserver.sockets.on('connection', function (socket) {
    socket.on('my event', function (content) {
        console.log(content);
    });
    socket.on('clientMessage', function(content) {
        socket.emit('serverMessage', 'You said: ' + content);
        socket.get('username', function(err, username) {
            if (! username) {username = socket.id;}
            socket.get('room', function(error, room) {
                if (error) {throw error;}
                var broadcast = socket.broadcast;
                var message = content;
                if (room) {
                    broadcast.to(room);
                }
                broadcast.emit('serverMessage', username + ' said: ' + message);
            });
        });
    });
    socket.on('join', function(room) {
        socket.get('room', function(err, oldRoom) {
            if (error) {throw error;}
            socket.set('room', room, function(error) {
                if (error) {throw error;}
                socket.join(room);
                if (oldRoom) {socket.leave(oldRoom);}
                socket.get('username', function(err, username) {
                    if (! username) {username = socket.id;}
                });
                socket.emit('serverMessage', 'You joined room ' + room);
                socket.get('username', function(err, username) {
                    if (! username) {username = socket.id;}
                    socket.broadcast.to(room).emit('serverMessage', 'User ' + username + ' joined this room');
                });
            });
        });
    });
    socket.on('disconnect', function() {
        socket.get('username', function(err, username) {
            if (! username) {username = socket.id;}
            socket.broadcast.emit('serverMessage', 'User ' + username + ' disconnected');
        });
    });
    socket.on('login', function(username) {
        socket.set('username', username, function (error) {
            if (error) {throw error;}
            socket.emit('serverMessage', 'Currently logged in as ' + username);
            socket.broadcast.emit('serverMessage', 'User ' + username + ' logged in');
        });
    });
    socket.emit('login');
});

ioserver.listen(4000);

// Websocket Client

<html>
    <head>
        <title>Socket.IO example application</title>
        <script src="http://localhost:4000/socket.io/socket.io.js"></script>
        <script>
            var socket = io.connect('http://localhost:4000');
            socket.on('serverMessage', function(content) {
                console.log(content);
            });
            socket.on('login', function() {
                var username = prompt('What username would you like to use?');
                socket.emit('login', username);
            });
            socket.emit('my event', 'Hello world.');
        </script>
    </head>
    <body></body>
</html>

// WebSocket Server with Standart Server

var fs = require('fs');

var http = require('http');
var ioserver = require('socket.io');

var server = http.createServer();

server.on('request', function (request, response) {
    fs.readFile(__dirname + '/index.html', function(error, data) {
        if (error) {
            res.writeHead(500);
            res.end('Error loading index.html');
        } else {
            res.writeHead(200, 'OK');
            res.end(data);
        }
    });
});

ioserver.sockets.on('connection', function (socket) {
    socket.on('my event', function(content) {
        console.log(content);
    });
    socket.on('clientMessage', function (content) {
        socket.emit('serverMessage', 'You said: ' + content);
        socket.get('username', function(err, username) {
            if (! username) {username = socket.id;}
            socket.broadcast.emit('serverMessage', username + ' said: ' + content);
        });
    });
    socket.on('disconnect', function() {
        socket.get('username', function(err, username) {
            if (! username) {username = socket.id;}
            socket.broadcast.emit('serverMessage', 'User ' + username + ' disconnected');
        });
    });
    socket.on('login', function(username) {
        socket.set('username', username, function (error) {
            if (error) {throw error;}
            socket.emit('serverMessage', 'Currently logged in as ' + username);
            socket.broadcast.emit('serverMessage', 'User ' + username + ' logged in');
        });
    });
    socket.emit('login');
});

server.listen(4000);
ioserver.listen(4000);

// Path

path.normalize('/foo//bar//baz/asdf/quux/..'); // '/foo/bar/baz/asdf'
path.join('/foo', 'bar', 'baz/asdf'); // '/foo/bar/baz/asdf'
path.resolve('/foo/bar', '../baz'); // 'C:\foo\baz'
path.relative('/data/test/aaa', '/data/impl/bbb'); // '../bbb'

path.dirname('/foo/bar/baz/asdf/quux.txt'); // '/foo/bar/baz/asdf'
path.basename('/foo/bar/baz/asdf/quux.html'); // 'quux.html'

// Files

fs.exists('/does_not_exist', function (exists) {
    console.log('exists:', exists);
});

fs.stat('/etc/passwd', function(err, stats) {
    console.log(stats.isFile());
    console.log(stats.isDirectory());
    console.log(stats.isBlockDevice());
    console.log(stats.isCharacterDevice());
    console.log(stats.isSymbolicLink());
    console.log(stats.isFifo());
    console.log(stats.isSocket());
});

fs.open('./my_file.txt', 'r', function opened(err, fd) {
    if (err) { throw err }
    var readBuffer = new Buffer(1024),
        bufferOffset = 0,
        bufferLength = readBuffer.length,
        filePosition = 100;
    fs.read(fd, readBuffer, bufferOffset, bufferLength, filePosition, function read (err, readBytes) {
        if (err) { throw err; }
        console.log('just read ' + readBytes + ' bytes');
        if (readBytes > 0) {console.log(readBuffer.slice(0, readBytes));}
        fs.close(fd);
    });
});

fs.open('./my_file.txt', 'a', function opened(err, fd) {
    if (err) { throw err; }
    var writeBuffer = new Buffer('writing this string'),
        bufferPosition = 0,
        bufferLength = writeBuffer.length, filePosition = null;
    fs.write(fd, writeBuffer, bufferPosition, bufferLength, filePosition, function wrote(err, written) {
        if (err) { throw err; }
        console.log('wrote ' + written + ' bytes');
        fs.close(fd);
    });
});

// Streams

fs.open(path, 'r', function(err, fd) {
    fs.createReadStream(null, {fd: fd, encoding: 'utf8'});
    fd.on('data', function (chunk) {console.log(chunk);});
});

require('http').createServer(function(req, res) {
    var rs = fs.createReadStream('/path/to/big/file');
    rs.on('data', function(data) {
        var sent = res.write(data);
        if (!sent) {
            rs.pause();
        }
    });
    res.on('drain', function() {
        rs.resume();
    });
    rs.on('end', function() {
        res.end();
    });
}).listen(8080);

require('http').createServer(function(req, res) {
    var rs = fs.createReadStream('/path/to/big/file');
    rs.pipe(res, {end: false});
    rs.on('end', function() {
        res.write("And that's all, folks!");
        res.end();
    });
}).listen(8080);

require('http').createServer(function(req, res) {
    var rs = fs.createReadStream('/path/to/big/file');
    rs.pipe(res);
}).listen(8080);

// Process Swarm

node plus_one.js

// unpause the stdin stream
process.stdin.resume();
process.stdin.on('data', function( data) {
    var number;
    try {
        number = parseInt(data.toString(), 10); // parse the input data into a number
        number += 1; // increment by one
        process.stdout.write(number + "\n"); // output the number
    } catch (err) {
        process.stderr.write(err.message + "\n");
    }
});

node plus_one_test.js

var spawn = require('child_process').spawn;

var child = spawn('node', ['plus_one.js']); // Spawn the child with a node process executing the plus_one app

setInterval(function () { // Call this function every 1 second (1000 milliseconds):
    var number = Math.floor(Math.random() * 10000); // Create a random number smaller than 10.000
    child.stdin.write(number + "\n"); // Send that number to the child process:
    child.stdout.once('data', function (data) { // Get the response from the child process and print it:
        console.log('child replied to ' + number + ' with: ' + data);
    }); // Because you want to listen for data back from the child only once per number, you will use child.stdout.once and not child.stdout.on.
    // If you used the latter function, you would register multiple callback functions as time went by.
    // Each callback function would be called every time you got data from on the child stdout, which would end up printing the data multiple times and erroneously.
}, 1000);

child.stderr.on('data', function(data) {
    process.stdout.write(data);
});

child.on('exit', function(code, signal) {
    if (code) {
        console.log('child process terminated with code ' + code);
    } else if (signal) {
        console.log('child process terminated because of signal ' + signal);
    }
});

setTimeout(function() {
    child.kill();
}, 5000);


var spawn = require('child_process').spawn;
require('http').createServer(function(request, response) {
    var child = spawn('tail', ['-f', '/var/log/system.log']);
    child.stdout.pipe(res);
    res.on('end', function() {
        child.kill();
    });
}).listen(4000);

// Uncaught Exception

process.on('uncaughtException', function(err) {
    // do something
});

// MySQL

// npm install mysql

var mysql = require('mysql');

var client = mysql.createClient({
    host: 'localhost',
    user: 'root',
    password: 'root',
});

client.query('DROP DATABASE IF EXISTS node');

client.query('CREATE DATABASE node');

client.query('USE node');

client.query('CREATE TABLE test ' +
                 '(id INT(11) AUTO_INCREMENT, ' +
                 ' content VARCHAR(255), ' +
                 ' PRIMARY KEY(id))'
);

for (var i = 0; i < 10000; i++) {
    client.query('INSERT INTO test (content) VALUES (?)', ['content for row ' + (i + 1)]);
}

client.query('UPDATE test SET content = ? WHERE id >= ?', ['new content', 9000], function (err, info) {
    console.log('Changed content of ' + info.affectedRows + ' rows');
});

query = client.query('SELECT id, content FROM test WHERE id >= ? AND id <= ?', [8990, 9010]);

query.on('error', function(err) {throw err;});

query.on('row', function(row) {console.log('Content of row #' + row.id + ' is: "' + row.content + '"');});

query.on('end', function(result) {console.log('Finished retrieving results');});

client.end();

Комментариев нет:

Отправить комментарий