码迷,mamicode.com
首页 > Web开发 > 详细

NodeJS POST Request Over JSON-RPC

时间:2015-12-14 18:59:22      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

1.npm install art-template
2.npm  install request
3.在app.js中加入以下代码转html:

var template = require(‘art-template‘);
template.config(‘base‘, ‘‘);
template.config(‘extname‘, ‘.html‘);
app.engine(‘.html‘, template.__express);
app.set(‘view engine‘, ‘html‘);

4.在routes/index.js中

 

var express = require(‘express‘);
var router = express.Router();
//var request=require(‘request‘);
var request = require("request");
/* GET home page. */
router.get(‘/‘, function(req, res, next) {
var headers = {
‘Content-Type‘: ‘application/x-www-form-urlencoded‘,
‘Accept‘: ‘application/json-rpc‘,
‘keyname‘:‘XXX‘
};
var options = {
url: "XXX",
method: ‘POST‘,
headers: headers,
form: {jsonrpc:‘{"method": "XXX", "params": ["XXXXX"], "id": X}‘}
};
request(options, function (error, response, body) {
res.render("index", {title:res.statusCode.toString() + " " + body});
});
});

module.exports = router;

将对应的keynameurlmethod、params、id改为自己的值
运行结果:

200 {"jsonrpc":"2.0","result":"XXXXX测试数据","id":X}

 

NodeJS POST Request Over JSON-RPC

标签:

原文地址:http://www.cnblogs.com/yingzi1028/p/5045949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!