0

Node.js で Web フックを作成する最も簡単な方法は何ですか? (URL に投稿)。

ありがとう

4

3 に答える 3

3

Node.jsホームページから:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');

reqオブジェクトにアクセスして、データを取得できます。

より高レベルのアプローチについては、express.jsをチェックしてください。

次のようなことができます。

var app = express.createServer();

app.post('/', function(req, res){
    res.send('Hello World');
});

app.listen(3000);
于 2011-04-17T17:11:15.683 に答える
3
var options = {
  host: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'POST',
  headers: ...
};

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

http.requestドキュメントから。

基本的に、メソッドを使用して、ホスト/ポート + パスへの意見ハッシュでリクエストできます。次に、そのサーバーからの応答を処理します。

于 2011-04-17T13:21:20.267 に答える
0

node.jsモジュールのRESTlerを強くお勧めします。

rest.post('http://user:pass@service.com/action', {
    data: { id: 334 },
}).on('complete', function(data, response) {
    if (response.statusCode == 201) {
        // you can get at the raw response like this...
    }
});
于 2012-02-08T21:32:25.513 に答える