是这样的
现在要用android 传递 http post 请求到 node.js
以前我试过用php接收http请求 , 它是这样子的:
$data = $_post['data'];
android則是这样的
httppost httprequest = new httppost(192.168.1.174:3001); list params = new arraylist(); 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的方法?
回复内容: 是这样的
现在要用android 传递 http post 请求到 node.js
以前我试过用php接收http请求 , 它是这样子的:
$data = $_post['data'];
android則是这样的
httppost httprequest = new httppost(192.168.1.174:3001); list params = new arraylist(); 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的方法?
支持楼上建议的使用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);});
有 但是系统自带的post处理比较复杂 具体使用方法可以谷歌一下 推荐使用web框架,比如说express
单用express也不是很方便,题主还可以试试加上一个叫multer的中间件,今天刚试过,挺好用的