我正在学习无服务器后端,我正在尝试使用webtask.io部署一个简单的后端来从加密货币交换中获取价格。我在本地部署服务器时工作正常,但是当我尝试在webtask.io中部署它时,我收到此错误Cannot find module './server/routes/api'
。
您可以在此处查看错误:
这是我的代码
index.js
var Express = require('express');
var Webtask = require('webtask-tools');
var bodyParser = require('body-parser')
const app = Express();
const api = require('./server/routes/api');
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(express.static(path.join(__dirname, 'dist')));
// Set our api routes
app.use('/api', api);
// Catch all other routes and return the index file
app.get((req, res, next) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
module.exports = Webtask.fromExpress(app);
api.js
const express = require('express');
const router = express.Router();
// declare axios for making http requests
const axios = require('axios');
const coinTicker = require('coin-ticker');
/* GET api listing. */
router.get('/', (req, res, next) => {
res.send('api works');
});
router.get('/clpbtc', function(req, res, next) {
axios.get('https://www.surbtc.com/api/v2/markets/btc-clp/ticker')
.then(response => {
res.status(200).json(response.data);
})
.catch(error => {
console.log(error);
});
});
router.get('/ethbtc', function(req, res, next) {
coinTicker('bittrex', 'ETH_BTC')
.then(posts => {
res.status(200).json(posts.bid);
})
.catch(error => {
res.status(500).send(error);
});
});
router.get('/ltcbtc', function(req, res, next) {
coinTicker('bittrex', 'LTC_BTC')
.then(posts => {
res.status(200).json(posts.bid);
})
.catch(error => {
res.status(500).send(error);
});
});
router.get('/xrpbtc', function(req, res, next) {
coinTicker('bittrex', 'XRP_BTC')
.then(posts => {
res.status(200).json(posts.bid);
})
.catch(error => {
res.status(500).send(error);
});
});
router.get('/zecbtc', function(req, res, next) {
coinTicker('bittrex', 'ZEC_BTC')
.then(posts => {
res.status(200).json(posts.bid);
})
.catch(error => {
res.status(500).send(error);
});
});
router.get('/timer', function(req, res, next) {
setInterval(function () {
console.log("Hello");
}, 5000);
})
module.exports = router;
感谢您一如既往的帮助!
答案 0 :(得分:0)
我很难找到解决此问题的方法,因为Webtask文档中没有很好地记录它。
解决此问题的关键是在wt-cli中使用--bundle
标志
示例
faucet.js 文件
const bitgoHandler = require('./functions/bitgoHandler')
const to = require('await-to-js').default
module.exports = faucet
async function faucet ({body, data}, cb) {
const [err, result] = await to(bitgoHandler.sendTransaction(body, data))
if (err) return cb(err)
cb(null, result)
}
bitgoHandler.js 文件
const BitGoJS = require('bitgo');
const to = require('await-to-js').default
module.exports = { getWallet, sendTransaction }
function getWallet (accessToken, faucetWalletId) {
const bitgo = new BitGoJS.BitGo({ env: 'test', accessToken });
const coin = bitgo.coin('tbtc');
return coin.wallets().get({id: faucetWalletId})
}
async function sendTransaction ({receiverAddress}, {ACCESS_TOKEN, FAUCET_WALLET_ID, WALLET_PASSPHRASE}) { const [err, wallet] = await to(getWallet(ACCESS_TOKEN, FAUCET_WALLET_ID)) if (err) return Promise.reject(err)
let params = {
amount: 0.01 * 1e8,
address: receiverAddress,
walletPassphrase: WALLET_PASSPHRASE
};
return wallet.send(params)
}
要部署整个应用程序,我使用了以下脚本
wt create -s ACCESS_TOKEN=<ACCESS_TOKEN> -s FAUCET_WALLET_ID=<FAUCET> -s WALLET_PASSPHRASE=<WALLET> --bundle faucet.js
答案 1 :(得分:0)