CodeGenieApp / serverless-express

Run Express and other Node.js frameworks on AWS Serverless technologies such as Lambda, API Gateway, Lambda@Edge, and more.
https://codegenie.codes
Apache License 2.0
5.14k stars 667 forks source link

originalUrl changed after upgrading from 3.4.0 to 4.10.1 #544

Open ghost opened 2 years ago

ghost commented 2 years ago

Hit a problem upgrading from 3.4.0 to 4.10.1. I referenced your UPGRADE.md and examples.

Using aws-serverless-express@3.4.0, my url path for a particular REST GET call gets resolved to /profiles/envConfig/client-config-v1 as expected. I have some code at that routed path. It works.

Once I upgrade to @vendia/serverless-express@4.10.1, that same REST GET call unexpectedly gets resolved to /envConfig/client-config-v1, and since I don't manage that path, the call returns an error message: Cannot GET /envConfig/client-config-v1.

Any insight into what would cause the change in url path on the node serverless-express side? Wondering if no longer using app.use(middleware.eventContext()) or awsServerlessExpress.proxy changes url path resolution?

I'm providing essential code snippets. Any thoughts would be most appreciated. Thanks.


CODE using aws-serverless-express@3.4.0 works as expected.

index.js

const awsServerlessExpress = require('aws-serverless-express');
const app = require('./app');
const server = awsServerlessExpress.createServer(app);
exports.handler = (event, context) =>
  awsServerlessExpress.proxy(server, event, context, 'PROMISE').promise;

app.js

const express = require('express');
const middleware = require('aws-serverless-express/middleware');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());
app.use(middleware.eventContext());

app.get('/profile/envConfig/:envConfigId', async (req, res, next) => {
  // this works since url is /profiles/envConfig/client-config-v1
}

app.listen(3000);
module.exports = app;

Some console debug output:

res: {
  url: '/profiles/envConfig/client-config-v1',
  originalUrl: '/profiles/envConfig/client-config-v1',
  _parsedUrl.path: '/profiles/envConfig/client-config-v1',
  apiGateway: {
      event: {
        resource: '/profiles/{proxy+}',
        path: '/profiles/envConfig/client-config-v1',
        httpMethod: 'GET',
        ...
      },
  }
  ...
}

CODE using @vendia/serverless-express@4.10.1 fails. Fails whether I use the built-in router or instantiate my own app.Router().

index.js

const serverlessExpress = require('@vendia/serverless-express');
const app = require('./app');
exports.handler = serverlessExpress({ app });

app.js

const express = require('express');
const { getCurrentInvoke } = require('@vendia/serverless-express');
const bodyParser = require('body-parser');

const app = express();
const router = express.Router();
router.use(bodyParser.json());

router.get('/profiles/envConfig/:envConfigId', async (req, res, next) => {
  // this doesn't get called since path does not match
  const currentInvoke = getCurrentInvoke();
  ...
}

app.use('/', router);
module.exports = app;

Some console debug output:

res: {
    url: '/envConfig/client-config-v1',
    baseUrl: '',
    originalUrl: '/envConfig/client-config-v1',
    _parsedUrl.path: '/envConfig/client-config-v1',
}
ghost commented 2 years ago

In my code, a url of /myPath using 3.4.0 remains /myPath using 4.10.1.

But a url of /myPath/subpath using 3.4.0 becomes /subpath using 4.10.1.

thetumper commented 2 years ago

Duplicate of https://github.com/vendia/serverless-express/issues/400?