我们可以在 AWS Amplify 中使用 Express JS 吗?

Can we use Express JS in AWS Amplify?

我有一个由 aws-amplify 构建的应用程序,但现在我想添加 expressjs 作为服务器以添加一些服务器端逻辑。有可能做到吗?我无法从 aws amplify 文档中找到任何建议或描述。

有人可以帮助我吗?非常感谢

您可以使用 REST 或 GraphQL 使用 API 添加服务器端逻辑。为此,您可以将 amplify add apianplify push.

一起使用

如果你想使用 ExpressJS,你也可以在一个函数中使用它。

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