Next.js Discord

Discord Forum

Cannot read from req.body in serverless function

Answered
Barbary Lion posted this in #help-forum
Open in Discord
Barbary LionOP
I have a very simple express server hosted on vercel using serverless functions. You can follow this guide to get to where I am: https://vercel.com/guides/using-express-with-vercel#standalone-express

I then added 2 endpoints of my own.

I added this route which works perfectly, does not read from req.body though:

app.get('/create-room', function (req, res) {
    res.end(JSON.stringify(
        { "room_id": Math.random().toString(36).slice(-6) } 
    ));
});


The route having issues is this:

app.post('/auth', function (req, res) {
    const { body } = req;
    return res.end(`Hello ${body.name}, you just parsed the request body!`);
});


My vercel.json is this, I changed the source value on purpose, no bigge:
{
    "rewrites": [{ "source": "/(.*)", "destination": "/api" }]
  }


My deployed url to the broken endpoint is this: https://meeting-backend-vercel.vercel.app/auth, you can curl to it with curl -X POST "https://meeting-backend-vercel.vercel.app/auth" -H "Content-Type: application/json" -d '{ "name": "Reader" }'. You can curl to the /create-room endpoint which works fine with this: curl -X GET https://meeting-backend-vercel.vercel.app/create-room. The logs say every time I do the curl to /auth there is a internal server error as it can't read req.body. Why?
Answered by Barbary Lion
so express has built in json parsing, I had to activate it using app.use(express.json());
View full answer

23 Replies

Barbary LionOP
Nope
well iirc you need it as express doesn't have default parsing
at least a year ago
Barbary LionOP
I ran the server locally and didn't use body-parser and it worked fine
Also hosted on railway and it worked
I will try it out in the morning thanks for suggestion
also why do you want to use express on vercel anyway
it is kinda sketcky implmented... because it is trying to be serverless for a serverfull framework
Barbary LionOP
it's free
railway gonna cost me, a dollar a month maybe
I will see if it's free on render
yeah but express vs the default vercel api handler?
Barbary LionOP
there's a default vercel handler?
Til
I'm gonna use next js if I don't figure it out
Less hassle for serverleess functions
yeah, this is the first link i found of it https://vercel.com/docs/functions/serverless-functions
Barbary LionOP
found the solution
Barbary LionOP
so express has built in json parsing, I had to activate it using app.use(express.json());
Answer
Barbary LionOP
I had deleted this line unknowingly when I copied over my original express code and cut it down to make a minimal server
@riský do you have the body parser middleware on your express? https://expressjs.com/en/5x/api.html#req.body
Barbary LionOP
this also works if u install body-parser as a separate package
silly me I should have pasted my code into phind or gpt-help and it would have found the problem, I just pasted it in and it said to use express.json()