0

我正在尝试将 netlify lambda 函数与创建反应应用程序一起使用,它正在破坏我的网站。

该 repo 是通过运行npx create-react-app my-app-name创建的,并且是标准的 create react app 样板。

文件结构:

根目录/package.json

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "lambda": "netlify-lambda serve src/lambda"
  },
  "devDependencies": {
    "netlify-lambda": "^2.0.15"
  }

根目录/netlify.toml:


[build]
  command = "npm build" 
  functions = "lambda" 
  publish = "build"

src/setupProxy.js:


const proxy = require("http-proxy-middleware");

module.exports = function (app) {
  app.use(
    proxy("/.netlify/functions/", {
      target: "http://localhost:9000/",
      pathRewrite: {
        "^/\\.netlify/functions": "",
      },
    })
  );
};


src/lambda/dictionary.js:

exports.handler = (event, context, callback) => {
  callback(null, {
    statusCode: 200,
    body: "hello world",
  });
};

现在,当我尝试运行npm run start时,应用程序将无法加载。

浏览器显示错误:

This site can’t be reachedlocalhost refused to connect.

当您在浏览器中运行npm run lambda并访问 urlhttp://localhost:9000/.netlify/functions/dictionary时,浏览器会按预期显示“hello, world”。

此外,我无法使用 netlify cli,因为当我尝试安装它时,即使使用 sudo,我也会收到权限错误/访问被拒绝。因此,试图让这种非全局安装的方式工作。

4

0 回答 0