NodeJSWeb app listens to the sock file instance _ node. js

Source: Internet
Author: User
This article mainly introduces NodeJSWeb application listening to sock file instances. This article describes the example of NodeJS TCP and HTTP listening to DomainSocket files, you can refer to the TCP service written in NodeJS to listen to a sock File (Domain Socket), and the HTTP service can do the same. Although it is of little significance to connect to a sock file as an HTTP service, it is only a pure attempt.

The TCP service is written in this way.

The code is as follows:


Var net = require ('net ');
Net. createServer (function (socket ){
Socket. on ('data', function (data ){
Socket. write ('stored Ed: '+ data );
});
}). Listen ('/tmp/node_tcp.sock ');


Connect the above '/tmp/node_tcp.sock'

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!

Accurately speaking, this article should be NodeJS's TCP and HTTP listening Domain Socket files.

Domain Socket for TCP listening is still very common, for example, sometimes access to the local database or cache will do this, like using '/tmp/mysql. sock' to access the MySQL service on the local machine, so you do not need to start the TCP port exposure, the security is improved, and the performance is also improved.

Now let's take a look at the HTTP Listener of NodeJS on Domain Socket, which is transformed from a classic example.

The code is as follows:


Var http = require ('http ');
Http. createServer (function (req, res ){
Res. writeHead (200, {'content-type': 'Text/plain '});
Res. end ('Hello World \ n ');
}). Listen ('/tmp/node_http.sock ');
Console. log ('server running at/tmp/node_http.sock ');

I still do not know how to access the above HTTP service in the browser, so use telnet to test

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


Correctly process HTTP requests on '/tmp/node_http.sock.

Access through NodeJS HTTP Client

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


Run the above code. if the file name is http_client.js,

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 only records the content, but I still cannot think of the actual intention of making the HTTP service listen on Domain Socket, and the browser cannot 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.