英文:
Node.js: Send GET request via unix socket
问题
我是你的中文翻译助手,以下是翻译好的内容:
我对Node.js还不熟悉。我正在尝试使用Unix套接字建立客户端和服务器之间的连接,其中客户端请求使用Node.js,而后台运行的服务器使用Go语言。
客户端代码:
var request = require('request');
request('http://unix:/tmp/static0.sock:/volumes/list', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
} else {
console.log("在接收器的else部分" + response.statusCode + body);
}
});
当我尝试与使用Go语言编写的服务器通信时,显示出HTTP错误:400 Bad Request: malformed Host header
。
以下命令可以正常工作:
curl -X GET --unix-socket /tmp/static0.sock http://:/volumes/list
不确定我的请求有什么问题。我们需要发送头部吗?我期望得到JSON响应。
英文:
I am new to the node.js. I am trying to setup the client server connection using unix socket, where my client request would be in node.js and server running in the background would be in go.
Client side Code:
var request = require('request');
request('http://unix:/tmp/static0.sock:/volumes/list', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
} else {
console.log("In else part of the receiver" + response.statusCode + body)
}
}
})
When I try to communicate with the server written in go it is shows the HTTP error: 400 Bad Request: malformed Host header'
The same works with:
curl -X GET --unix-socket /tmp/static0.sock http://:/volumes/list
Not sure what is wrong with my request. Do we need to send the headers? I expecting the JSON response.
答案1
得分: 17
要实现这个而不使用request模块,可以尝试以下代码:
const http = require('http');
const options = {
socketPath: '/tmp/static0.sock',
path: '/volumes/list',
};
const callback = res => {
console.log(`状态码: ${res.statusCode}`);
res.setEncoding('utf8');
res.on('data', data => console.log(data));
res.on('error', data => console.error(data));
};
const clientRequest = http.request(options, callback);
clientRequest.end();
这段代码使用了Node.js的http模块来发送HTTP请求。它创建了一个客户端请求对象clientRequest
,并通过http.request
方法发送请求。请求的选项包括socketPath
和path
,分别指定了Unix域套接字路径和请求路径。回调函数callback
用于处理响应,包括打印状态码和响应数据。最后,通过调用clientRequest.end()
方法发送请求。
英文:
To accomplish this without using the request module, try the following:
const http = require('http');
const options = {
socketPath: '/tmp/static0.sock',
path: '/volumes/list',
};
const callback = res => {
console.log(`STATUS: ${res.statusCode}`);
res.setEncoding('utf8');
res.on('data', data => console.log(data));
res.on('error', data => console.error(data));
};
const clientRequest = http.request(options, callback);
clientRequest.end();
答案2
得分: -6
考虑以下示例:
客户端代码:
您还可以在某些代码中进行套接字连接:
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script>
socket = io.connect('http://www.example.com', {'force new connection':true});
socket.on('event', function(){
// 要执行的代码
});
</script>
Node.js 服务器端代码:
首先安装 npm,然后执行以下步骤:
- npm init
- npm install --save express
- npm install --save socket
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer();
var io = require('socket.io').listen(server);
var socket = null;
io.sockets.on('connection', function(soc) {
socket = soc;
console.log('socket connected');
});
app.get('/test', function(request, response) {
var message = "Hello world";
socket.emit('event', message);
response.writeHead(200);
response.write(message);
response.end();
});
var server = app.listen(8080, '0.0.0.0');
您可以在浏览器中访问并检查。
英文:
Consider the following example :
client side code :
Also you can do socket connection in some code :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script>
socket = io.connect('http://www.example.com' , {'force new connection':true});
socket.on('event', function(){
// code to execute
});
</script>
<!-- end snippet -->
server side code in nodejs :
For this install npm, then
1)npm init
2)npm install --save express
3)npm insatll --save socket
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer();
var io = require('socket.io').listen(server);
var socket = null;
io.sockets.on('connection', function(soc) {
socket = soc;
console.log('socket connected');
});
app.get('/test', function(request, response) {
var message = "Hello world";
socket.emit('event', message);
response.writeHead(200);
reponse.write(message);
reponse.end();
});
var server = app.listen(8080, '0.0.0.0');
<!-- end snippet -->
You can hit this from browser a check
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论