Home > Backend Development > PHP Tutorial > 关于Node.js 接收 Http POST

关于Node.js 接收 Http POST

WBOY
Release: 2016-06-06 20:36:47
Original
1147 people have browsed it

是这样的
现在要用Android 传递 Http post 请求到 Node.js
以前我试过用PHP接收Http请求 , 它是这样子的:

<code>$data = $_POST['data'];
</code>
Copy after login
Copy after login

Android則是这样的

<code> 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();
 }
</namevaluepair></namevaluepair></code>
Copy after login
Copy after login

可是在Node.js 我该怎样做?
Node.js 带没有类似$_POST的方法?

回复内容:

是这样的
现在要用Android 传递 Http post 请求到 Node.js
以前我试过用PHP接收Http请求 , 它是这样子的:

<code>$data = $_POST['data'];
</code>
Copy after login
Copy after login

Android則是这样的

<code> 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();
 }
</namevaluepair></namevaluepair></code>
Copy after login
Copy after login

可是在Node.js 我该怎样做?
Node.js 带没有类似$_POST的方法?

支持楼上建议的使用Express,几行代码就可以搞定了

<code>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");
});
</code>
Copy after login

有 但是系统自带的post处理比较复杂 具体使用方法可以谷歌一下 推荐使用web框架,比如说express

单用express也不是很方便,题主还可以试试加上一个叫multer的中间件,今天刚试过,挺好用的

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template