const express = require('express');
const { stdout } = require('process');
const router = express.Router();
router.get('/', (req, resp) => {
var exec = require('child_process').exec;
exec(
'npx hardhat run scripts/deploy-contract.mjs --network PolygonMumbai',
function (error, stdout, stderr) {
console.log('stdout: ' stdout);
console.log('stderr: ' stderr);
if (error !== null) {
console.log('exec error: ' error);
}
}
);
const smartcontract = stdout.toString().substring(30, 72);
console.log(smartcontract);
resp.send(smartcontract);
});
我是一個具有請求回應和穩定性的初學者。但我想做的是通過命令列執行部署合約并將輸出作為回應發送。基本上,我想構建一個 API,在呼叫時部署智能合約并將回應作為智能合約地址發送。目前我無法將字串作為回應發送。
另外請讓我知道這是否是正確的方法
uj5u.com熱心網友回復:
我相信您只需要在 exec 回呼中撰寫回應:
router.get('/', (req, resp)=>{
var exec = require('child_process').exec;
exec('npx hardhat run scripts/deploy-contract.mjs --network PolygonMumbai',
function (error, stdout, stderr) {
console.log('stdout: ' stdout);
console.log('stderr: ' stderr);
if (error !== null) {
console.log('exec error: ' error);
}
const smartcontract = stdout.toString().substring(30,72);
console.log(smartcontract);
resp.send(smartcontract)
});
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/487457.html
