Webtask.io backend cannot find module

271 Views Asked by At

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:

WT error link

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!

2

There are 2 best solutions below

0
On

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-cli

Example

faucet.js file

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 file

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) 
}

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

0
On

Just use the --bundle argument.

wt create --bundle index.js

Source: webtask docs