Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post Requests Freezing On Serverless Offline #1482

Closed
benjamin852 opened this issue Jun 23, 2022 · 6 comments
Closed

Post Requests Freezing On Serverless Offline #1482

benjamin852 opened this issue Jun 23, 2022 · 6 comments

Comments

@benjamin852
Copy link

benjamin852 commented Jun 23, 2022

Hi all. I am having the same hanging post error as in:
#1150

My get requests are working totally fine my post requests for some reason keep freezing even in a super simple test route I have

const test = require('express').Router({ mergeParams: true });

test.post('/', async (req, res, next) => {
  res.send(JSON.stringify({ key: 'hello this is the test route!' }));
});

module.exports = test;

Here is my serverless.yml file

service: my-app-server
plugins:
  - serverless-dotenv-plugin
  - serverless-webpack
  - serverless-offline
custom:
  webpack:
    webpackConfig: 'webpack.config.js'
    packager: 'npm'
    excludeFiles: test/**/*.test.js
    stats: 'verbose'
    includeModules: true # enable auto including modules
  serverless-offline:
    httpPort: ${env:HTTP_PORT, 4000}
provider:
  name: MyAppServer
  runtime: nodejs10.x
functions:
  api: 
    handler: src/index.handler
    timeout: 600
    events:
      - http: ANY /
      - http: "ANY {proxy+}"

I recall reading in the other issue I mentioned that it had something to do with a node version, and I am opening a new issue as the last comment on the other thread asked for people to open a new issue if they see this problem rather than re-open the old issue.

Anyway, my current node version that I am running is:

v18.4.0

Hope there is a fix for this soon if not I will just downgrade my node version, unless there is something else anyone can point out that I am doing wrong. Again my get requests are fine only my post requests are freezing.

Thank you.

@dnalborczyk
Copy link
Collaborator

dnalborczyk commented Jun 23, 2022

thank you for the repro @benjamin852 .

I don't think your issue has anything to do with the node version being used. at a quick glance, I would think it's the way you are using express, but hard to tell.

your handler also does not match the exported function. could you create a repository I can clone and have a look at? also, if you can, reduce the repro to the smallest possible you are able to achieve. e.g. remove webpack, dotenv plugin etc.

could you also add a working route with a get request, so I can also see something which works?

@benjamin852
Copy link
Author

Hey @dnalborczyk Thanks for the response.

Sorry I forgot to add my handler file being referenced in the yaml file

So here is my handler file:

// Bootstrap dotenv
require('./loadConfigs');

// if (process.env.NODE_ENV !== 'test') {}
require('./db/index');

const serverless = require('serverless-http');
const express = require('express');
const app = express();
const compression = require('compression');

if (process.env.NODE_ENV !== 'test') {
  app.use(compression());
}

const { cors } = require('./routes/middleware');
app.use('*', cors);
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

app.use('/test', require('./routes/test'));

// Default error handler for all routes
app.use((error, req, res, next) => {
  if (res.headersSent) {
    return next(error);
  }
  res.status(error.statusCode || 500).json({
    error: {
      name: error.name,
      message: error.message,
      data: error.data,
    },
  });
});

if (process.env.NODE_ENV === 'test') {
  const port = 3000 || process.env.HTTP_PORT;
  app.listen(port, () => console.log(`API started listening on PORT: ${port} `));
  module.exports = app;
} else {
  module.exports.handler = serverless(app);
}

Regarding the working get request I was referencing before it's as simple as switching the test route to this

const test = require('express').Router({ mergeParams: true });

test.get('/', async (req, res, next) => {
  res.send(JSON.stringify({ key: 'hello this is the test route!' }));
});

module.exports = test;

And then calling a get request from postman and it works fine.

The reason I suspect its my node version is because recently upgrade my node to the newest version, and since the upgrade was when I began experiencing this problem (originally the post requests were working fine), and the answers in the post I referenced before all mentioned node versioning problems.

I am trying to debug right now with this node version and I will downgrade node versions as a last resort if nothing else works to see if that fixes it.

Thanks again for the help please let me know if I have added enough code to make my problem clear.

@benjamin852
Copy link
Author

benjamin852 commented Jun 24, 2022

Okay so as referenced in (#1150) issue I downgraded my node to 15.4 and now post request seems to work fine.

I will leave this open temporarily as I am assuming there is a better way to fix this than having to downgrade to a node version from two years ago.

@dnalborczyk
Copy link
Collaborator

dnalborczyk commented Jun 25, 2022

I was able to successfully run your code with node v18.4 on macos for GET and POST, albeit with some modifications.

could you create a repository with your repro where I can have a look at?

@benjamin852
Copy link
Author

So I created a brand new repo with just the test route and it worked! So I am guessing now that it is something else in my app that was causing it to mess up, so I can try work backwards not to catch what it is, perhaps just some dependencies versioning. Odd that revert node back to old version fixed it though. Thank you for the help. I will close this for now once I figure out what was causing this I will post back with my fix.

@benjamin852
Copy link
Author

This was just a versioning issue with my package serverless-offline. Updated it to latest version and now post requests work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants