-
Notifications
You must be signed in to change notification settings - Fork 167
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
Exception: Error: Unexpected request.body type: undefined #255
Comments
Update: Its weird but when i send a raw data to the above api im getting the response hi i had changed the serverless.yml in this case to:
it was only working with /api/serverless any other routes its showing 404 not found is it broken or im doing it wrong |
I was able to get past this like this: req.rawBody = req.rawBody || '';
context.res = await handler(context, req); Also: In the |
Thank you for the response @TaylorBeeston After updating the code now its showing and had to update the serverless.yml to this
because i was getting 404 error in the old yml file any help is appreciated been stuck here since forever. Researching |
Okay, I've done a bunch of work myself getting this to run, and I've finally just gotten it working correctly both offline and deployed! To start, I've started using ESBuild to drastically lower the cycle time when deploying. I did that by first adding esbuild: pnpm i -D esbuild rimraf # You don't have to use pnpm, but I highly recommend it! Then adding a build script: // esbuild.mjs
import { build } from 'esbuild';
const startTime = Date.now();
console.log('🎁 Building main bundle...');
const finalBuildObj = {
entryPoints: ['src/index.ts'], // add whatever src files your handler is in!
platform: 'node',
bundle: true,
format: 'cjs',
outfile: 'dist/index.js',
target: 'node12',
plugins: [],
external: [],
minify: true,
};
if (process.env.NODE_ENV !== 'production') {
finalBuildObj.sourcemap = 'inline';
finalBuildObj.minify = false;
}
build(finalBuildObj).then(() => {
console.log(`🎁 Done building main bundle! (${Date.now() - startTime}ms)`);
}); Then with that in place, I wrapped the sls commands to add a build step first: // package.json
"scripts": {
"test": "echo \"No tests yet...\"",
"build": "rimraf dist && node esbuild.mjs",
"sls-deploy": "pnpm build && sls deploy",
"sls-offline": "pnpm build && sls offline",
"start": "pnpm sls-offline"
}, (Again, you don't have to use pnpm. Just replace With this in place, you'll use The last thing to do is update your serverless file to only include the built code: # serverless.yaml
package:
patterns:
- "!**"
- dist/**
- app/**
- host.json
- local.settings.json This change brought my "minimal" One more thing: You'll now be using the built js file inside of # serverless.yaml
functions:
app:
handler: dist/index.app If that part is confusing to you, I can elaborate by showing you my directory structure, as well as what gets put inside of Okay, with ESBuild out of the way, it should be much easier for you to start testing a config that works for you. I found that adding the manual // host.json
{
"version": "2.0"
} So, in the end, my service: azure
frameworkVersion: '3'
provider:
name: azure
region: West US 2
runtime: nodejs12
plugins:
- serverless-azure-functions
package:
patterns:
- "!**"
- dist/**
- app/**
- host.json
- local.settings.json
functions:
app:
handler: dist/index.app
events:
- http: true
route: "{test}"
method: ANY
cors: true
authLevel: anonymous I have a simple express app that exposes |
Just to be extra helpful, I decided to upload my code here! |
Thank you for the response and for a working example @TaylorBeeston currently i have changed the folder structure a little bit and goes like app.ts
handler.ts
routes.ts:
not able to hit either hello in the handler.ts or routes in routes.ts meanwhile i'll be also trying to rectify what silly mistake i would had done |
Update: The routes are working in postman but not in normal URL's |
With the same code of the issue https://github.com/dougmoscrop/serverless-http/issues/252 I'm getting an request.body error which says undefined.
The context parameter log:
the only one api im using:
the error:
I'm also trying to implement and debug as the azure is untested
Extra added line to debug in hello.js :
this didnt resolve the issue
The text was updated successfully, but these errors were encountered: