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;

猜你喜欢

转载自blog.csdn.net/qq_25944759/article/details/107922064