所以我有一个简单的客户端应用程序与node.js中的服务器端应用程序通信.在客户端,我有以下代码:
function send (name) {
http.request({
host: '127.0.0.1',
port: 3000,
url: '/',
method: 'POST'
}, function (response) {
response.setEncoding('utf8');
response.on('data', function (data) {
console.log('did get data: ' + data);
});
response.on('end', function () {
console.log('\n \033[90m request complete!\033[39m');
process.stdout.write('\n your name: ');
});
response.on('error', function (error) {
console.log('\n Error received: ' + error);
});
}).end(query.stringify({ name: name})); //This posts the data to the request
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,如果我不通过以下方式包含'数据'事件:
response.on('data', function (data) {
console.log('did get data: ' + data);
});
Run Code Online (Sandbox Code Playgroud)
响应的"结束"事件永远不会被触发.
服务器代码如下:
var …Run Code Online (Sandbox Code Playgroud) 大家好我刚刚开始学习node.js并在互联网上搜索很多东西,然后尝试在node.js中编码我使用这两个代码向我显示相同的结果,但最后一个是在我的浏览器上显示错误喜欢"无法找到页面"的东西.所以请向我解释原因?
// JScript source code
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
Run Code Online (Sandbox Code Playgroud)
这是有效的
// Include http module.
var http = require("http");
// Create the server. Function passed as parameter is called on every request made.
// request variable holds all request parameters
// response variable allows you to do anything with response sent to the client.
http.createServer(function (request, response) {
// Attach listener on end event. …Run Code Online (Sandbox Code Playgroud) 我正在制定一个狡猾的计划,该计划涉及使用 node.js 作为另一个服务前的代理服务器。
简而言之:
我有基本的工作,但现在试图让整个事情与 Sencha Connect一起工作,这样我就可以访问提供的所有中间件。
所有的动作都发生在下面的dispatchProxy中
connect(
connect.logger(),
connect.static(__dirname + '/public'),
(request, response) ->
dispatchProxy(request, response)
).listen(8000)
dispatchProxy = (request, response) ->
options = {host: host, port: port, method: request.method, headers: request.headers, path: request.url}
proxyRequest = http.request(options, (proxyResponse) ->
proxyResponse.on('data', (chunk) ->
response.write(chunk, 'binary')
)
proxyResponse.on('end', (chunk) ->
response.end()
)
response.writeHead proxyResponse.statusCode, proxyResponse.headers
)
request.on('data', (chunk) ->
proxyRequest.write(chunk, 'binary')
)
# this is never triggered for GETs
request.on('end', ->
proxyRequest.end()
)
# so …Run Code Online (Sandbox Code Playgroud)