NodeJS POST Request Over JSON-RPC

艾茂学
2023-12-01

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;

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

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

 

转载于:https://www.cnblogs.com/yingzi1028/p/5045949.html

 类似资料: