小编Cha*_*nge的帖子

http-proxy-middleware 是否可以与 Serverless Lambda 配合使用?

我正在尝试通过无服务器 Lambda 代理外部 API。尝试以下代码示例:http://localhost:3000/users/1返回 200 但正文为空。我必须忽略一些东西,因为http://localhost:3000/users/11返回 404 (如预期)。

索引.js

'use strict';
const serverless = require('serverless-http');
const express = require('express');
const {
  createProxyMiddleware
} = require('http-proxy-middleware');

const app = express();

const jsonPlaceholderProxy = createProxyMiddleware({
  target: 'http://jsonplaceholder.typicode.com',
  changeOrigin: true,
  logLevel: 'debug'
});

app.use('/users', jsonPlaceholderProxy);

app.get('/', (req, res) => {
  res.json({
    msg: 'Hello from Serverless!'
  })
})

const handler = serverless(app);
module.exports.handler = async (event, context) => {
  try {
    const result = await handler(event, context);
    return result;
  } …
Run Code Online (Sandbox Code Playgroud)

proxy http aws-lambda http-proxy-middleware serverless

5
推荐指数
1
解决办法
1276
查看次数