https服务搭建

一、 构建Https服务器

新建工程

$ cd /home

$ express -e ExpressServer

$ cd ExpressServer

$ sudo npm install

生成证书文件

# 创建一个文件夹存放证书

$ mkdir cert

$ cd cert

 

#生成私钥key文件:

$ openssl genrsa -out privatekey.pem 1024

 

#通过私钥生成CSR证书签名

$ openssl req -new -key privatekey.pem -out certsign.csr

 

# 通过私钥和证书签名生成证书文件

$ openssl x509 -req -in certsign.csr -signkey privatekey.pem -out certificate.crt

生成了三个文件:

 

privatekey.pem (私钥)

certsign.csr (CSR证书签名)

certificate.crt (证书文件)

Express服务端代码

var app = require('express')();

var fs = require('fs');

var http = require('http');

var https = require('https');

 

var httpServer = http.createServer(app);

var httpsServer = https.createServer({

key: fs.readFileSync('./cert/privatekey.pem', 'utf8'),

cert: fs.readFileSync('./cert/certificate.crt', 'utf8')

}, app);

 

var PORT = 8000;

var SSLPORT = 9000;

 

httpServer.listen(PORT, function() {

console.log('HTTP Server is running on: http://localhost:%s', PORT);

});

httpsServer.listen(SSLPORT, function() {

console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT);

});

 

// 访问路径

app.get('/:name', function(req, res) {

if(req.protocol === 'https') {

res.send('https:' + req.params.name);

} else {

res.send('http:' + req.params.name);

}

});

 

 

 

server服务代码

server.js

var express = require('express'); // call express

var app = express(); // define our app using express

var bodyParser = require('body-parser');

var https = require('https')

var fs = require('fs');

 

var path = require('path');

var util = require('util');

var os = require('os');

const { FileSystemWallet, Gateway } = require('fabric-network');

 

const ccpPath = path.resolve(__dirname, '..', '..', 'first-network', 'connection-org1.json');

var contract;

try {

 

// Create a new file system based wallet for managing identities.

const walletPath = path.join(process.cwd(), 'wallet');

const wallet = new FileSystemWallet(walletPath);

console.log(`Wallet path: ${walletPath}`);

 

// Check to see if we've already enrolled the user.

wallet.exists('user1').then((userExists)=>{

if (!userExists) {

console.log('An identity for the user "user1" does not exist in the wallet');

console.log('Run the registerUser.js application before retrying');

return;

}

 

// Create a new gateway for connecting to our peer node.

const gateway = new Gateway();

gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } }).then(()=>{

 

// Get the network (channel) our contract is deployed to.

gateway.getNetwork('mychannel').then((network)=>{

contract = network.getContract('mycc');

console.log('contract 生成')

 

})})})

 

app.use(bodyParser.urlencoded({ extended: false }));

app.use(bodyParser.json());

 

 

require('./routes.js')(app);

var httpsServer = https.createServer({

key: fs.readFileSync('./cert/privatekey.pem', 'utf8'),

cert: fs.readFileSync('./cert/certificate.crt', 'utf8')

}, app);

 

var port = 8000;

httpsServer.listen(port, function() {

console.log('HTTPS Server is running on: https://10.0.8.93:%s', port);

});

 

} catch (error) {

console.error(`Failed to evaluate transaction: ${error}`);

process.exit(1);

}

var evaluateTransaction = async function(id) {

return await contract.evaluateTransaction('query',id);

}

var submitTransaction = async function(payer,recipient,amount){

return await contract.submitTransaction('invoke',payer,recipient,amount);

}

exports.evaluateTransaction = evaluateTransaction;

exports.submitTransaction = submitTransaction;

 

routes.js

module.exports = function(app){

app.get('/query', require('./routes/query.js')); //get 请求

app.post('/invoke', require('./routes/invoke.js')); //post 请求

}

 

package.json

{

"name": "fabcar",

"version": "1.0.0",

"description": "FabCar application implemented in JavaScript",

"dependencies": {

"fabric-ca-client": "^1.4.2",

"fabric-network": "^1.4.2",

"grpc": "^1.6.0",

"express": "latest",

"body-parser": "latest"

},

"devDependencies": {

"chai": "^4.2.0",

"eslint": "^5.9.0",

"mocha": "^5.2.0",

"nyc": "^14.1.1",

"sinon": "^7.1.1",

"sinon-chai": "^3.3.0"

}

}

 

query.js

var express = require('express'); // call express

var app = express(); // define our app using express

var bodyParser = require('body-parser');

 

var server = require('./../server.js');

var router = express.Router();

 

router.get('/query',function(req,res) {

console.log("query: ");

var id = req.query.id

server.evaluateTransaction(id).then((result)=>{

res.send(result.toString());

})

});

 

module.exports = router;

 

invoke.js

var express = require('express'); // call express

var server = require('./../server.js');

var router = express.Router();

 

router.post('/invoke/',function(req,res) {

console.log("invoke: ");

var payer = req.body.payer

var recipient = req.body.recipient

var amount = req.body.amount

server.submitTransaction(payer,recipient,amount).then((result)=>{

res.send('Success!');

})

});

module.exports = router;

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sbfmjscs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值