Nodejs Web Application Listener sock file instance _node.js

Source: Internet
Author: User
Tags require

A TCP service like Nodejs can listen on a sock file (Domain Socket), and its HTTP service can do the same. Although it makes little sense to connect to a sock file as an HTTP service, this is a pure attempt.

The TCP service is written like this

Copy Code code 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 the top '/tmp/node_tcp.sock '
Copy Code code 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 exact, this should be the Nodejs TCP and HTTP listening Domain Socket file.

For TCP listening Domain Socket is also very common, such as sometimes to the local database or cache access will do so, like using '/tmp/mysql.sock ' to access the local MySQL service, so that no need to start TCP port exposed, security has improved, performance on also has certain promotion.

Now look at the Nodejs HTTP listening on Domain Socket, from the classic example to transform

Copy Code code as follows:

var http = require (' http ');
Http.createserver (function (req, res) {
Res.writehead ({' Content-type ': ' Text/plain '});
Res.end (' Hello world\n ');
}). Listen ('/tmp/node_http.sock ');
Console.log (' Server running At/tmp/node_http.sock ');

I'm not sure how to access the HTTP services above in the browser, so use Telnet to test

Copy Code code 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 OK
Content-type:text/plain
Date:mon, 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 '.

Using Nodejs HTTP Client to access

Copy Code code 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 filename is http_client.js,
Copy Code code as follows:

Node Http_client.js
status:200
HEADERS: {"Content-type": "Text/plain", "date": "Mon, no more than 2015 04:25:49 GMT", "Connection": "Close", "transfer-encoding ":" Chunked "}
Hello World

This article is only recorded, and it is not yet possible to have the HTTP service listen to the actual purpose of the Domain Socket, and the browser is unable to access it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.