简体   繁体   English

“内部服务器错误”无服务器节点Rest API

[英]“Internal server error” serverless node rest api

i'm writing a serverless node rest api, and i have few functions and today i faced an issue on sending responds from lambda function to api gateway, my callback doesn't work as it expected, what i am doing wrong? 我正在编写一个无服务器的节点剩余api,并且我的功能很少,今天我遇到了从lambda函数向api网关发送响应的问题,我的回调无法正常工作,我在做什么错?

module.exports.create = (event, context, callback) => {

  client.on('connect', () => {
      console.log("connected to redis");
      callback(null, {
        statusCode: 200,
        headers: { 'Content-Type': 'text/plain' },
        body: 'connection established.',
      });
      return;
  });

};

Ok, I've encountered Internal server error a few times before and I suggest you to do this. 好的,我之前几次遇到Internal server error ,建议您这样做。

First, a little background knowledge you must have: 首先,您必须具备一些背景知识:

When you're deploying your serverless application, what is happening under the hood is that serverless framework creates necessary configurations and .zip file (your lambda functions code and dependencies) under .serverless folder. 部署serverless应用程序时,实际上是serverless framework.serverless文件夹下创建了必要的配置和.zip文件(您的lambda函数代码和依赖项)。

So if you're missing necessary dependencies in your package.json or forget to include them in the .zip file, your lambda will return Internal server error . 因此,如果您在package.json缺少必要的依赖项,或者忘记将它们包含在.zip文件中,则lambda将返回Internal server error

And you should check whether you included dependencies into dev-dependencies in package.json , too. 并且您还应该检查是否在package.json中将dependencies包含在dev-dependencies中。 (This will prevent your necessary modules to be included in .zip file). (这将防止您的必要模块包含在.zip文件中)。

And secondly, if you're using serverless-webpack plugin, you should include these lines in your serverless.yaml file. 其次,如果你使用serverless-webpack插件,你应该在你的这些行serverless.yaml文件。

custom:
  webpack:
    includeModules: true

This worked for my case. 这适用于我的情况。

If you don't understand or have anything to ask, feel free to do that :) 如果您不理解或有任何疑问,请随时这样做:)

A common issue people have with Lambda and NodeJS is timing... I think what's happening here is that the Lambda Function terminates before your response comes back. 人们对Lambda和NodeJS的一个普遍问题是计时...我想这里发生的是Lambda函数在您的响应返回之前就终止了。 Lambda does not wait around for an async response, so most of the time doesn't execute the responds events, so never hits your callback. Lambda不会等待异步响应,因此大多数时间不会执行响应事件,因此永远不会触发您的回调。

Try using a Promise, which keeps the code/Lambda running until the async call comes back and the callback is called. 尝试使用Promise,它将使代码/ Lambda保持运行状态,直到异步调用返回并且调用了回调。

This is a good article on how to achieve that: 这是一篇有关如何实现这一目标的好文章:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

Internal errors are when the return isn't hit for some reason, it can be a coding error or a timeout because the default serverless timeout is too low for what you are trying to do. 内部错误是指由于某种原因未返回时,可能是编码错误或超时,因为默认的无服务器超时对于您尝试执行的操作而言太低了。

If you want to alter the timeout you can do something like this in the serverless.yml: 如果要更改超时,可以在serverless.yml中执行以下操作:

functions:
  create:
    handler: handler/create
    timeout: 30
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM