у меня есть Node.js сервер: -связи между сервером и клиентом с node.js
// *********** Server that receives orders ************ //
// to use features of the http protocol. //
var http = require('http');
// initialize to empty string. //
var req = "";
// create the server that will receive an order Request. //
var server = http.createServer(function(req,res) {
res.writeHead(200, {'content-type': 'text/plain'});
// when data is successfully received, a success message is displayed. //
res.on('data', function(data){
req += data; // received data is appended. //
console.log("We have received your request successfully.");
});
});
// An error message is displayed - error event. //
server.on('error', function(e){
console.log("There is a problem with the request:\n" + e.message);
});
// server listens at the following port and localhost (IP). //
server.listen(8000, '127.0.0.1');
, а затем у меня есть Node.js Клиент: -
var http = require("http");
var querystring = require("querystring");
var postOrder = querystring.stringify({
'msg': 'Hello World!'
});
var options = {
hostname: '127.0.0.1',
port: 8000,
path:'/order',
method:'POST',
headers:{
'Content-Type' :'application/x-www-form-urlencoded',
'Content-Length' : postOrder.length
}
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write(postOrder);
req.end();
Я нахожусь пытаясь выяснить, как я могу заставить клиента опубликовать свой заказ на сервере и получить ответ с сервера ... либо сообщение об успешном завершении, либо сообщение об ошибке ... используя командную строку.
В настоящее время я бегу сервер на CMD строке $ узла server.js
, а затем запустить клиент $ узел client.js
, но я не получаю ответов.
Я предполагаю, что вы оставили из 'вар HTTP = требуется («HTTP»), и' вар строки запроса = требуется (» querystring ");' from client.js при публикации? – Randy
есть. извините, я отредактирую текст и добавлю его в сообщение. – Philo
- проблема, потому что я указал путь в клиенте, '/ order', но у моего сервера нет пути, упомянутого? – Philo