Node js tutorials
Today I learned
Total response time
To calculate the overall response time of the method we basically use new Date() function but in these functions we cannot able to find the exact time. if we use the process.hrtime() function we can find the difference accurately till nano seconds
Process.hrtime()
const NS_PER_SEC = 1e9;
const time = process.hrtime();
// [ 1800216, 25 ]
setTimeout(() => {
const diff = process.hrtime(time);
// [ 1, 552 ]
console.log(`Benchmark took ${diff[0] * NS_PER_SEC + diff[1]} nanoseconds`);
// benchmark took 1000000552 nanoseconds
}, 1000);
HTTP & HTTPS request in nodejs
There are two modules in nodejs to send the request http, https (for secure request)
There is another module called url in nodejs its used for parse, Create request object, find path , resolve path
var http= require('http')
var https = require('https')
var url = require('url').URL;
/**
* Http request
*/
function httpRequest(options, callback) {
console.log('Options', options)
var request = http.get(options, function (res) {
var data = '';
//Read the data chunk by chunk
res.on('data', function (chunk) {
data += chunk;
})
//At the end of the request
res.on('end', function () {
console.log("Page download completed...." , data)
callback('success', data)
})
//Error will throw if url not found..etc
res.on('error', function (err) {
console.log("page error", err)
callback('error', err)
})
})
//To handle the Address not found exception
request.on('error', function (err) {
console.log('Request error ==>', err);
callback('error', err)
});
request.end();
}
/**
* Simpe https request
* @param {Object} options its contains port, url, username etc
* @param {*} callback return data after req complete
*
*
options {
http: 'https:',
host : 'en.wikipedia.org',
port : 443,
path : '/wiki/Node.js'
method : 'GET'
}
*
*/
function httpsRequest(options, callback) {
var request = https.get(options, function (res) {
var data = '';
//Read the data chunk by chunk
res.on('data', function (chunk) {
data += chunk;
})
//At the end of the request
res.on('end', function () {
console.log("Page download completed....")
callback('success', data)
})
//Error will throw if url not found..etc
res.on('error', function (err) {
console.log("page error", err)
callback('error', err)
})
})
//To handle the Address not found exception
request.on('error', function (err) {
console.log('Request error ==>', err);
callback('error', err)
});
request.end();
}
var page = 'https://en.wikipedia.org/wiki/Node.js'
// httpsRequest(new url(page) , function(status, data){
// console.log('status==>', status, 'Data ==>', data.length )
// })
// httpRequest(new url('http://192.168.8.221:7077/final/#!'), function (status, data) {
// console.log('Http url status==>', status, 'Data ==>', data.length)
// })
// httpRequest(new url('http://192.21:7077/final/#!'), function (status, data) {
// console.log('Http url status==>', status, 'Data ==>', data)
// })
// var page = 'https://en.widia.org/wiki/Node.js'
// httpsRequest(new url(page), function (status, data) {
// console.log('status==>', status, 'Data ==>', data.length)
// })
Memory limit in nodejs
v8 has a memory limit of 512mb on 32-bit systems, and 1gb on 64-bit systems. The limit can be raised by setting --max-old-space-size to a maximum of ~1gb (32-bit) and ~1.7gb (64-bit), but it is recommended that you split your single process into several workers if you are hitting memory limits.
Thread pool size in nodejs
Its default size is 4, but it can be changed at startup time by setting the
UV_THREADPOOL_SIZE
environment variable to any value (the absolute maximum is 128).
Comments
Post a Comment