Home  >  Article  >  Web Front-end  >  NodeJS web application listening sock file example_node.js

NodeJS web application listening sock file example_node.js

WBOY
WBOYOriginal
2016-05-16 16:13:411643browse

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

Copy code The code is as follows:

var net = require('net');
net.createServer(function (socket) {
socket.on('data', function (data) {
​ socket.write('received: ' data);
});
}).listen('/tmp/node_tcp.sock');

Connect to the one above '/tmp/node_tcp.sock'
Copy code The code is as follows:

telnet /tmp/node_tcp.sock
Trying /tmp/node_tcp.sock...
Connected to (null).
Escape character is '^]'.
Hello World!
received: Hello World!

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

Copy code The code is as follows:

var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
}).listen('/tmp/node_http.sock');
console.log('Server running at /tmp/node_http.sock');

I don’t know yet how to access the above HTTP service in the browser, so use telnet to test

Copy code The code is as follows:

telnet /tmp/node_http.sock
Trying /tmp/node_http.sock...
Connected to (null).
Escape character is '^]'.
GET/HTTP/1.1
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Mon, 26 Jan 2015 04:21:09 GMT
Connection: keep-alive
Transfer-Encoding: chunked

c
Hello World

0


Can correctly handle HTTP requests on '/tmp/node_http.sock'.

Use NodeJS HTTP Client to access

Copy code The code is as follows:

var http = require('http');

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();


Execute the above code, if the file name is http_client.js,
Copy code The code is as follows:

node http_client.js
STATUS: 200
HEADERS: {"content-type":"text/plain","date":"Mon, 26 Jan 2015 04:25:49 GMT","connection":"close","transfer-encoding":"chunked" }
Hello World

This article is just for the record. I can’t think of the actual purpose of letting the HTTP service listen on the Domain Socket, and the browser cannot access it.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn