带有无服务器离线阶段的 aws-serverless-express 中断路由

aws-serverless-express with serverless-offline stage breaks routing

我正在使用无服务器包: - aws-serverless-express - 无服务器离线

当我 运行宁 sls offline

我把所有东西都正确地送到了运行,但是我得到的路径是:

ANY | http://localhost:3001/dev/
ANY | http://localhost:3001/dev/{proxy+}

serverless.yaml 是

functions:
  app:
    handler: src/lambda.handler
    events:
      - http:
          path: /
          method: ANY
          cors: true
      - http:
          path: /{proxy+}
          method: ANY
          cors: true

我知道我有 "stage" env 我可以设置更改 但我正在寻找它的快速路线:

//this wont work
app.get('/r', (req, res) => {
  res.send('ready');
})

//this will work
app.get('/dev/r', (req, res) => {
  res.send('ready');
})

但是在生产中或者如果我使用任何其他 "stage" 如果我不在舞台上添加前缀,我的路由将无法工作。

有什么想法吗?

谢谢

基于文档:

sls offline --noPrependStageInUrl

应该适合你

或通过配置:

custom:
    serverless-offline:
        noPrependStageInUrl: true

Available Cli options:

--apiKey                    Defines the API key value to be used for endpoints marked as private Defaults to a random hash.
--corsAllowHeaders          Used as default Access-Control-Allow-Headers header value for responses. Delimit multiple values with commas. Default: 'accept,content-type,x-api-key'
--corsAllowOrigin           Used as default Access-Control-Allow-Origin header value for responses. Delimit multiple values with commas. Default: '*'
--corsDisallowCredentials   When provided, the default Access-Control-Allow-Credentials header value will be passed as 'false'. Default: true
--corsExposedHeaders        Used as additional Access-Control-Exposed-Headers header value for responses. Delimit multiple values with commas. Default: 'WWW-Authenticate,Server-Authorization'
--disableCookieValidation   Used to disable cookie-validation on hapi.js-server
--enforceSecureCookies      Enforce secure cookies
--hideStackTraces           Hide the stack trace on lambda failure. Default: false
--host                  -o  Host name to listen on. Default: localhost
--httpPort                  Http port to listen on. Default: 3000
--httpsProtocol         -H  To enable HTTPS, specify directory (relative to your cwd, typically your project dir) for both cert.pem and key.pem files
--ignoreJWTSignature        When using HttpApi with a JWT authorizer, don't check the signature of the JWT token. This should only be used for local development.
--lambdaPort                Lambda http port to listen on. Default: 3002
--noPrependStageInUrl       Don't prepend http routes with the stage.
--noAuth                    Turns off all authorizers
--noTimeout             -t  Disables the timeout feature.
--prefix                -p  Adds a prefix to every path, to send your requests to http://localhost:3000/[prefix]/[your_path] instead. Default: ''
--printOutput               Turns on logging of your lambda outputs in the terminal.
--resourceRoutes            Turns on loading of your HTTP proxy settings from serverless.yml
--useChildProcesses         Run handlers in a child process
--useWorkerThreads          Uses worker threads for handlers. Requires node.js v11.7.0 or higher
--websocketPort             WebSocket port to listen on. Default: 3001
--webSocketHardTimeout      Set WebSocket hard timeout in seconds to reproduce AWS limits (https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#apigateway-execution-service-websocket-limits-table). Default: 7200 (2 hours)
--webSocketIdleTimeout      Set WebSocket idle timeout in seconds to reproduce AWS limits (https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#apigateway-execution-service-websocket-limits-table). Default: 600 (10 minutes)
--useDocker                 Run handlers in a docker container.
--layersDir                 The directory layers should be stored in. Default: ${codeDir}/.serverless-offline/layers'
--dockerReadOnly            Marks if the docker code layer should be read only. Default: true
--allowCache                Allows the code of lambda functions to cache if supported.