2013-12-09 26 views
0

薩拉姆(手段你好):)如何在node.js中向Apache Syncope API提交POST請求?

在我的node.js應用程序中,我試圖通過其RESTful API向Apache Syncope添加用戶。 Apache Syncope安裝在遠程VPS上,this article在PHP環境中演示此過程。這裏是我在我的Node.js應用所做的:

http.createServer(onRequest).listen(80); 

function onRequest (request, response) { 
    var options = { 
     hostname: MY_VPS_IP, 
     port: 8080, 
     path: '/syncope/rest/user/create.json', 
     auth: 'admin:password', 
     method: 'POST' 
    }; 
    var data = {an: 'object', of: 'new', user: 'data' }; 

    var req=http.request(options, function(res) { 
     var body; 
     res.on('data', function (chunk) { 
      body += chunk; 
     }); 
     res.on('end', function() { 
      response.writeHead(200, { 'Content-Type': 'text/html' }); 
      response.end(body, 'utf-8'); 
     }); 
    }); 
    req.end(JSON.stringify(data)); 
} 

阿帕奇暈厥的響應是415錯誤(CSS樣式略):

undefined 
<html> 
<head><title>Apache Tomcat/7.0.42 - Error report</title> 
</head> 
<body><h1>HTTP Status 415 - </h1> 
<HR size="1" noshade="noshade"> 
<p><b>type</b> Status report</p> 

<p><b>message</b> <u></u></p> 

<p><b>description</b> <u>The server refused this request because the request entity is in a format not supported by the 
    requested resource for the requested method.</u></p> 
<HR size="1" noshade="noshade"> 
<h3>Apache Tomcat/7.0.42</h3></body> 
</html> 
+0

節點使用chuncked傳輸編碼,這裏不支持暈渲。由於您可能已經運行了nginx,只需將它粘貼在暈厥之前即可。 – Gant

+0

@DamonGant我對nginx不熟悉,你可以解釋一下'堅持在暈厥前面'是什麼意思嗎?謝謝。 –

回答

1

的Node.js將用於chuncked傳輸編碼發送請求,這是HTTP 1.1的一部分。在Node.js裏面解決這個問題既不需要,也不需要快速或優雅。一個常見的解決方案是通過nginx代理不兼容的服務(這將重新編碼請求)。這是一個非常簡單的反向代理配置示例。

server { 
    listen 80; 
    server_name syncope.example.com; 
    proxy_pass http://example.com:8080/; 
    allow 127.0.0.1; # Allow local connections, can be any IP(+CIDR) too 
    deny all; 
} 
+0

Apache Syncope安裝在Apache Tomcat上,有沒有辦法通過重新配置Tomcat和不使用nginx來處理這種情況? –

+0

@NasserTorabzade不知道,但你希望nginx在你的節點應用程序之前,以及服務靜態文件或使用[乘客](https://github.com/phusion/passenger/wiki/Phusion-Passenger:-Node.js -tutorial) – Gant