我正在尝试通过无服务器 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)