【发布时间】:2022-10-15 19:57:05
【问题描述】:
我有以下问题... 事实证明,我无法通过 nodejs 的无服务器框架中的无服务器离线插件在本地测试我的 aws lambda 函数。无法执行简单的 GET 请求。
我可以使用 serverless deploy 命令部署到 aws lambda,但是为了获得更好的团队开发体验,我们需要使用 serverless-offline 在本地部署,我遵循了本指南 https://fauna.com/blog/develop-using-serverless-offline,但是当我做一个简单的成功运行serverless offline 命令后获取请求。
这是我在成功运行serverless offline 时得到的
但是当我通过邮递员对链接http://localhost:3000/ 发出获取请求时
我收到以下错误
它基本上说找不到 node:url 模块,但我做了 npm install url,但仍然抛出同样的错误。 我将在下面插入项目树和文件:
handler.js
//"use strict";
console.log("AJAJAJAJAJAJAJAJAJAAJAJ");
console.log("Printing module");
console.log(module.exports);
module.exports.hello = async (event) => {
console.log("whaaaaat");
return {
statusCode: 200,
body: JSON.stringify(
{
message: "Go Serverless v3.0! Your function executed successfully!",
input: event,
},
null,
2
),
};
};
无服务器.yml
org: ortizjorge97
app: aws-node-http-api-project
service: aws-node-http-api-project
frameworkVersion: '3'
provider:
name: aws
runtime: nodejs14.x
plugins:
- serverless-offline
- serverless-bundle
- serverless-dotenv-plugin
functions:
hello:
handler: handler.hello
events:
- httpApi:
path: /
method: get
包.json
{
"name": "aws-node-http-api-project",
"version": "1.0.0",
"description": "<!-- title: 'AWS Simple HTTP Endpoint example in NodeJS' description: 'This template demonstrates how to make a simple HTTP API with Node.js running on AWS Lambda and API Gateway using the Serverless Framework.' layout: Doc framework: v3 platform: AWS language: nodeJS authorLink: 'https://github.com/serverless' authorName: 'Serverless, inc.' authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4' -->",
"main": "handler.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"serverless-bundle": "^5.5.0",
"serverless-dotenv-plugin": "^4.0.2",
"url": "^0.11.0"
},
"devDependencies": {
"serverless-offline": "^10.2.0"
}
}
我是aws的新手,所以我不知道会发生什么。
我在用
- 节点 14.17.2
- MacOS Monterey m1 处理器
【问题讨论】:
-
当您尝试从 nodejs cli 中获取 url 时会得到什么。您能否检查一下 url 模块是否存在于 node_modules/ 目录中?
标签: node.js aws-lambda serverless serverless-framework serverless-offline