dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.71k stars 164 forks source link

Getting an error of Exception: TypeError: stream.listeners is not a function from Serverless node-module #252

Open Vikas252 opened 1 year ago

Vikas252 commented 1 year ago

I am currently using serverless-http for azure with express while going through the state i got an error from serverless node module. I have integrate it with serverless framework.

Here is the error i'm getting: Screenshot (2)

And this is my server setup and serverless-http setup hello.js:

"use strict"

const serverless = require("serverless-http")
const express = require("express")

const app = express()
const router = express.Router()
app.set("port", process.env.PORT || 3000 )
app.listen(app.get("port"), () => {
  console.log(`listening on port ${app.get("port")}`)
})

router.get("/api/serverless", (req, res) => {
  res.json("Hi")
})

app.use("/", router)

const handler = serverless(app, { provider: 'azure' });
module.exports.handlertest = async (context, req) => {
  context.res = await handler(context, req);
}

// Export your express server so you can import it in the lambda function.
module.exports = app

// app.use(/* register your middleware as normal */)

serverless.yml:

# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
#    docs.serverless.com
#
# Happy Coding!

service: serverless

# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
frameworkVersion: "3"

provider:
  name: azure
  region: West US 2
  runtime: nodejs14
  # os: windows  # windows is default, linux is available
  # prefix: "sample"  # prefix of generated resource name
  # subscriptionId: 
  # stage: dev
  # type: premium  # premium azure functions

  environment: # these will be created as application settings
    VARIABLE_FOO: "foo"

  # you can define apim configuration here
  # apim:
  #   apis:
  #     - name: v1
  #       subscriptionRequired: false # if true must provide an api key
  #       displayName: v1
  #       description: V1 sample app APIs
  #       protocols:
  #         - https
  #       path: v1
  #       tags:
  #         - tag1
  #         - tag2
  #       authorization: none
  #   cors:
  #     allowCredentials: false
  #     allowedOrigins:
  #       - "*"
  #     allowedMethods:
  #       - GET
  #       - POST
  #       - PUT
  #       - DELETE
  #       - PATCH
  #     allowedHeaders:
  #       - "*"
  #     exposeHeaders:
  #       - "*"

plugins: # look for additional plugins in the community plugins repo: https://github.com/serverless/plugins
  - serverless-azure-functions

# you can add packaging information here
package:
  patterns:
    # - '!exclude-me.js'
    # - '!exclude-me-dir/**'
    - "!local.settings.json"
    - "!.vscode/**"
    # - include-me.js
    # - include-me-dir/**

functions:
  api:
    handler: src/handlers/hello.handlertest
    events:
      - http: true
        name: res
        route: "{*segments}"
        authLevel: anonymous # can also be `function` or `admin`
      - http: true
        x-azure-settings:
          direction: out
          name: "$return"

  # The following are a few examples of other events you can configure:
  # storageBlob:
  #   handler: src/handlers/storageBlob.printMessage
  #   events:
  #     - blob:
  #       x-azure-settings:
  #         name: blob # Specifies which name is available on `context`
  #         path: blob-sample/{blobName}
  #         connection: AzureWebJobsStorage # App Setting/environment variable which contains Storage Account Connection String
  # storageQueue:
  #   handler: src/handlers/storageQueue.printMessage
  #   events:
  #     - queue: queue-sample
  #       x-azure-settings:
  #         name: message # Specifies which naem is available on `context`
  #         connection: AzureWebJobsStorage
  # timer:
  #   handler: src/handlers/timer.printMessage
  #   events:
  #     - timer:
  #       x-azure-settings:
  #         schedule: '*/10 * * * * *'
  # eventhub:
  #   handler: src/handlers/eventHub.printMessage
  #   events:
  #     - eventHub:
  #       x-azure-settings:
  #         name: eventHubMessages # Specifies which name it's available on `context`
  #         eventHubName: sample-hub # Specifies the Name of the Event Hub
  #         consumerGroup: $Default # Specifies the consumerGroup to listen with
  #         connection: EVENT_HUBS_CONNECTION # App Setting/environment variable which contains Event Hubs Namespace Connection String
  # serviceBusQueue:
  #   handler: src/handlers/serviceBusQueue.printMessage
  #   events:
  #     - serviceBus:
  #       x-azure-settings:
  #         name: message # Specifies which name is available on `context`
  #         queueName: sample-queue # Name of the service bus queue to consume
  #         connection: SERVICE_BUS_CONNECTION # App Setting/environment variable variable which contains Service Bus Namespace Connection String
  # serviceBusTopic:
  #   handler: src/handlers/serviceBusTopic.printMessage
  #   events:
  #     - serviceBus:
  #       x-azure-settings:
  #         name: message # Specifies which name it's available on `context`
  #         topicName: sample-topic # Name of the service bus topic to consume
  #         subscriptionName: sample-subscription # Name of the topic subscription to retrieve from
  #         connection: SERVICE_BUS_CONNECTION # App Setting/environment variable variable which contains Service Bus Namespace Connection String

function.json:

{
  "disabled": false,
  "bindings": [
    {
      "type": "httpTrigger",
      "direction": "in",
      "name": "res",
      "route": "{*segments}",
      "authLevel": "anonymous"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ],
  "entryPoint": "handlertest",
  "scriptFile": "../src/handlers/hello.js"
}

packages used: Azure Functions Core Tools Core Tools Version: 3.0.4837 Commit hash: N/A (64-bit) Function Runtime Version: 3.14.1.0

node version: 14.20.1 running in dev container using docker configuration