What's the easiest way of creating a Web Hook in Node.js? (POST to a URL).
Thanks
What's the easiest way of creating a Web Hook in Node.js? (POST to a URL).
Thanks
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文档。
基本上,您可以使用方法向主机/端口+路径请求意见散列。然后处理来自该服务器的响应。
从 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);
我强烈推荐 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...
}
});