I'm learning serverless backend and I'm trying webtask.io to deploy a simple backend to get the prices from cryptocurrency exchanges. My server is working fine when I deploy it locally, but when I try to deploy it in webtask.io I'm getting this error Cannot find module './server/routes/api'
.
You can check the error here:
this is my code
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;
Thanks for your help as always!
I had a hard time to find out the solution for this issue because it is not so well documented in Webtask docs.
The key for to solve this issue is to use the
--bundle
flag in wt-cliExample
faucet.js file
bitgoHandler.js file
To deploy the whole application I've used the script below
wt create -s ACCESS_TOKEN=<ACCESS_TOKEN> -s FAUCET_WALLET_ID=<FAUCET> -s WALLET_PASSPHRASE=<WALLET> --bundle faucet.js