Search code examples
expressaws-amplify

Can we use Express JS in AWS Amplify?


I have an app which build by aws-amplify, but now I want to add expressjs as server to add some server side logic. Is it possible to do it? I cannot find any suggestion or description for this from aws amplify document.

Can someone help me? Many thanks


Solution

  • You can add server side logic using an API either using REST or GraphQL. In order to do this you can use amplify add api together with anplify push.

    If you want to use ExpressJS you can also do it within a function.

    app.use(function(req, res, next) {
      res.header("Access-Control-Allow-Origin", "*")
      res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
      next()
    });
    // below the last app.use() method, add the following code 👇
    const axios = require('axios')
    
    app.get('/coins', function(req, res) {
      let apiUrl = `https://api.coinlore.com/api/tickers?start=0&limit=10`
    
      console.log(req.query);
      if (req && req.query) {
        const { start = 0, limit = 10 } = req.query
        apiUrl = `https://api.coinlore.com/api/tickers/?start=${start}&limit=${limit}`
      }
      axios.get(apiUrl)
        .then(response => {
          res.json({
            coins: response.data.data
          })
        })
        .catch(err => res.json({ error: err }))
    })
    

    Workshop