Home > Article > Web Front-end > NodeJS web application listening sock file example_node.js
TCP services written in NodeJS can listen on a sock file (Domain Socket), and its HTTP service can also do the same. Although it doesn't make much sense to connect to a sock file as an HTTP service, so this is just a pure attempt.
TCP service is written like this
To be precise, this article should be the TCP and HTTP monitoring Domain Socket files of NodeJS.
It is still very common for TCP monitoring Domain Socket. For example, sometimes this is done when accessing the local database or cache, such as using '/tmp/mysql.sock' to access the local MySQL service, so there is no need to start it. The TCP port is exposed, security is improved, and performance is also improved.
Now let’s take a look at NodeJS’s HTTP monitoring on Domain Socket, modified from a classic example
I don’t know yet how to access the above HTTP service in the browser, so use telnet to test
c
Hello World
0
Use NodeJS HTTP Client to access
var options = {
socketPath: '/tmp/node_http.sock',
Method: 'GET',
path: '/'
};
var req = http.request(options, function(res){
console.log('STATUS: ' res.statusCode);
console.log('HEADERS: ' JSON.stringify(res.headers));
res.on('data', function (chunk){
console.log(chunk.toString());
});
});
req.end();