我正在学习无服务器后端,我正在尝试 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;
一如既往地感谢您的帮助!