关于Node.js 接收 Http POST
是这样的
现在要用Android 传递 Http post 请求到 Node.js
以前我试过用PHP接收Http请求 , 它是这样子的:
$data = $_POST['data'];
Android則是这样的
HttpPost httpRequest = new HttpPost("192.168.1.174:3001");
List<NameValuePair > params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("data",etv.getText().toString()));
try{
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient()
.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200){
String strResult = EntityUtils.toString( httpResponse.getEntity() );
Log.d("HTTP", strResult);
}
} catch (Exception e){
e.printStackTrace();
}
可是在Node.js 我该怎样做?
Node.js 带没有类似$_POST的方法?
四月的苹果
9 years, 11 months ago
Answers
支持楼上建议的使用Express,几行代码就可以搞定了
var http = require("http");
var express = require("express");
var app = express();
app.post('/route_you_want', function(req, res){
//你可以在这里处理post请求
res.send("hello world");
res.end();
});
http.createServer(app).listen(3000, function(){
console.log("server start");
});
米饭·点点
answered 9 years, 11 months ago