serverless-express icon indicating copy to clipboard operation
serverless-express copied to clipboard

originalUrl changed after upgrading from 3.4.0 to 4.10.1

Open ghost opened this issue 2 years ago • 2 comments

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

Using [email protected], 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/[email protected], 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 [email protected] 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/[email protected] 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 avatar Jul 28 '22 08:07 ghost

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.

ghost avatar Jul 28 '22 08:07 ghost

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

thetumper avatar Aug 11 '22 17:08 thetumper