我正试图在我的lambda中间件上使用middy,我的堆栈是用TypeScrip和无服务器v3、Node.js v18创建的.

这里有一个最小的生殖例子:

tsconfig.json:

{
  "compilerOptions": {
    "preserveConstEnums": true,
    "strictNullChecks": true,
    "sourceMap": true,
    "allowJs": true,
    "target": "es5",
    "module": "CommonJS",
    "outDir": ".build",
    "moduleResolution": "node",
    "esModuleInterop": false,
    "lib": ["es2015"],
    "rootDir": "./"
  }
}

一百:

{
  "main": "handler.js",
  "dependencies": {
    "@middy/core": "^4.7.0",
    "lodash": "^4.17.21"
  },
  "devDependencies": {
    "@types/lodash": "4.14.91",
    "@types/node": "^11.13.0",
    "serverless-offline": "^13.3.2",
    "serverless-plugin-typescript": "^1.1.7",
    "typescript": "^5.3.3"
  }
}

serverless.yml:

service: serverless-example

plugins:
  - serverless-plugin-typescript
  - serverless-offline

provider:
  name: aws
  runtime: nodejs18.x

functions:
  hello:
    handler: handler.handler
    events:
      - http:
          path: hello
          method: get

handler.ts

从‘@middy/core’导入Middy

Const myProtectedFunction=async(事件,上下文)=>{

  const userId = "123"
  return {
      statusCode: 200,
      body: JSON.stringify({ message: `Hello, user ${userId}!` }),
  };

};

导出常量处理程序=middy() .hander(MyProtectedFunction);

在安装无服务器依赖后,只需执行:无服务器离线

然后向端点发出请求

Error image: error get

推荐答案

以下是对我起作用的方法:

package.json

{
  "name": "s3-ts-1",
  "dependencies": {
    "@middy/core": "^4.7.0",
    "lodash": "^4.17.21"
  },
  "type": "module",
  "devDependencies": {
    "@types/lodash": "4.14.91",
    "@types/node": "^11.13.0",
    "serverless": "^3.38.0",
    "serverless-offline": "^13.3.2",
    "serverless-plugin-typescript": "^2.1.5",
    "typescript": "^5.3.3"
  }
}

变更:

  • 指定type
  • 添加了serverless(这是因为我在全局范围内有v2,所以在项目范围内添加了v3)
  • 更新了开发依赖中的包

tsconfig.json

{
  "compilerOptions": {
    "preserveConstEnums": true,
    "strictNullChecks": true,
    "sourceMap": true,
    "allowJs": false,
    "target": "es2021",
    "outDir": ".build",
    "moduleResolution": "node",
    "lib": ["es2015"],
    "baseUrl": "./",
    "esModuleInterop": true,
    "typeRoots": ["node_modules/@types"],
    "resolveJsonModule": true
  }
}

变更: I made some changes from the middy doc here

serverless.ymlhandler.ts:不变

运行命令:npx sls offline(因为我为v3添加了本地依赖项.您可能不需要npx).

Node.js相关问答推荐

Mongoose查询-如何根据当前查找ID获取其他集合并将其插入到当前查找中?

为什么在导出的函数中调用node-sqlite3中的数据库方法时不起作用?

一个函数中的两个依赖的NodeJS数据库操作.如果第二个失败了怎么办?

如何从基于JSON的HTML/SCSS模板生成PDF?

通过PutObjectCommand上传AWS S3 PDF文件,结果为空PDF

通过 Node js 中的 TBA 执行 netsuite REST upsert 操作出现 401 错误

为什么我的过滤器无法在我在下面编写的 Google Analytics 4 应用程序脚本代码中工作?我该如何修复它?

如何在 JavaScript 中显示多维数组中使用的一维数组的变量名?

如何获取文件的中间值?

Node.js 上的 CLI 应用程序如何通过 child_process 将选项值作为参数传递给 Shell 命令

每秒从套接字传来的数据有哪些存储方式?

Nodejs mongoose 在一个查询中从多个集合中获取结果

如何在 Nest.js 中使用查询参数?

Nodejs-console.error vs util.debug

如何以编程方式检测nodejs中的调试模式?

Puppeteer 错误:未下载 Chromium 修订版

如何使用 gulp-uglify 缩小 ES6 函数?

密码的 Node.js 散列

node --experimental-modules,请求的模块不提供名为的导出

如何在 node.js 沙箱中安全地运行用户提交的脚本?