NestJs | Set max header size - nestjs

I am implementing API in NestJs and new to this framework. For one of my API I am getting error as
Status Code: 431 Request Header Fields Too Large
To fix this issue I want to change max header size in NestJs configuration. Where do I change this configuration in NestJs application.

This question is already answered for node here and for nest you can pass the argument as same as for node:
nest start -- --max-http-header-size=80000
As it's explained here
NestJS is a framework for NodeJS, you can pass NodeJS params directly, so I think this could be useful for you NodeJS command line options
And in case you want to set it from the code you can follow the same idea as this post explains something bodyparser is available for both.
I hope this is useful for you.

I could not get this to work by forwarding the node argument the way it's done in the currently accepted answer. Perhaps this is specific to my environment, but I was able to get it working using Nest's exec argument:
nest start --exec "node --max-http-header-size=40960"
The official description of the exec argument:
Binary to run (default: node).
https://docs.nestjs.com/cli/usages#nest-start

Related

Nestjs versioning configuration when using Fastify

For performance reasons of a relatively complex Nestjs application, we have chosen to use Fastify as our HTTP provider.
We are at a stage where we need to version out api and are running into problems after following instructions on the standard Nestjs guide:
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter(fastifyInstance),
{},
);
app.enableVersioning();
The error received is:
Property 'enableVersioning' does not exist on type 'NestFastifyApplication'.ts(2339)
I haven't been able to find a solution anywhere and thought I'd ask and see if anyone else has had the same problem and found a solution.
looks like you need to upgrade #nestjs/common because enableVersioning does exists in NestFastifyApplication:
https://github.com/nestjs/nest/blob/d5b6e489209090544a4f39c4f4a716b9800ca6a8/packages/platform-fastify/interfaces/nest-fastify-application.interface.ts#L16
https://github.com/nestjs/nest/blob/d5b6e489209090544a4f39c4f4a716b9800ca6a8/packages/common/interfaces/nest-application.interface.ts#L47

Debug compilation errors errors using Angular Universal in Angular 8 Application

A Brief Backstory
I have been implementing several upgrades to an Angular 8 application such as server-side rendering, and Google Analytics. As most developers do, I would code then test then move on to the next task. Typically I use ng serve to run the application as I am developing.
With Server-side rendering, to test speed, lazy-loaded images, etc, you need to use a node express server running on a generated JS file. After building, etc, I use Node prerender (my js file is prerender.js) to see what the application will look like prerendering on the server.
When I run this command, I should not get any errors, and I know my prender file will start a local server on port 4000.
The Problem
I get errors when running a node express server that I do not get when running with ng serve I recently got an error that said:
Unhandled Promise rejection: Cannot read property 'subscribe' of undefined ; Zone: <root> ; Task: Promise.then ; Value: TypeError: Cannot read property 'subscribe' of undefined
at new ApplicationRef (C:\4towerdevelopment\dist-stage\server\main.js:45910:37)
at _createClass (C:\4towerdevelopment\dist-stage\server\main.js:37184:20)
at _createProviderInstance (C:\4towerdevelopment\dist-stage\server\main.js:37138:26)
at initNgModule (C:\4towerdevelopment\dist-stage\server\main.js:37044:32)
at new NgModuleRef_ (C:\4towerdevelopment\dist-stage\server\main.js:38176:9)
at Object.createNgModuleRef (C:\4towerdevelopment\dist-stage\server\main.js:38159:12)
at NgModuleFactory_.create (C:\4towerdevelopment\dist-stage\server\main.js:50821:25)
at C:\4towerdevelopment\dist-stage\prerender.js:29175:43
at ZoneDelegate.invoke (C:\4towerdevelopment\dist-stage\prerender.js:481:26)
at Object.onInvoke (C:\4towerdevelopment\dist-stage\prerender.js:28683:33) TypeError: Cannot read property 'subscribe' of undefined
at new ApplicationRef (C:\4towerdevelopment\dist-stage\server\main.js:45910:37)
at _createClass (C:\4towerdevelopment\dist-stage\server\main.js:37184:20)
at _createProviderInstance (C:\4towerdevelopment\dist-stage\server\main.js:37138:26)
at initNgModule (C:\4towerdevelopment\dist-stage\server\main.js:37044:32)
at new NgModuleRef_ (C:\4towerdevelopment\dist-stage\server\main.js:38176:9)
at Object.createNgModuleRef (C:\4towerdevelopment\dist-stage\server\main.js:38159:12)
at NgModuleFactory_.create (C:\4towerdevelopment\dist-stage\server\main.js:50821:25)
at C:\4towerdevelopment\dist-stage\prerender.js:29175:43
at ZoneDelegate.invoke (C:\4towerdevelopment\dist-stage\prerender.js:481:26)
at Object.onInvoke (C:\4towerdevelopment\dist-stage\prerender.js:28683:33)
The closest this gets me to figuring what actually is causing the problem is letting me know that a provider somewhere is causing this error. Looks like something should be an observable rather than a subscription. Beyond that, I guess I just start looking through my providers. My question is:
How can I force Angular to possibly throw this error when developing using ng serve?
If I can't, is there a better way to debug this current error besides combing through each provider? Or at least a way to tell what service is causing the issue?
Thank you.
UPDATE: Basic repo with problem here. I made a new angular project, made sure dependencies were up to date, installed ngUniversal per this post, and received this same Unhandled promise message when running node prerender
Verbatim I went to to the Angular cli website, made a new project (default Angular version installed was 8.3), installed Angular Universal, and tried to build. Same error message as above.
This looks like it was ApplicationRef that triggered the error, and that class is provided internally by the Angular core.
It could be failing in the constructor of the class, and there are a few calls to subscribe on Zone observables. I don't think you're going to find anything in your source code that directly relates to this error. It looks like a build configuration problem.
https://github.com/angular/angular/blob/bb52fb798c8578c461d21aee2b7623232184a5d3/packages/core/src/application_ref.ts#L562
I do not know what could possibly produce this problem, but I would start a new project with SSR and compare the differences to your current project.
Eventually, I dropped the angular 6 approach using pre-render, and went with the latest universal package. There seems to be no problem building using npm run build:ssr and serving dist/server.js in an express server. I am not sure what the problem was with the pre-render approach, but it seems to be outdated anyways. #Reactgular thanks for the feedback.

Stripe with Webtask

I've been working for days on implementing Stripe Checkout into my static site. My static site is generated by React. There is a tutorial for setting up Stripe Checkout using React, however I also need to move the backend function to webtask.
Here is the tutorial. https://www.robinwieruch.de/react-express-stripe-payment/#express-stripe-backend
Any idea on how to port this over to webtask?
It should be pretty much the same thing, except that the author in this tutorial is:
Exporting an express server. To use that with webtask you're going to need to explicitly define the programming model with the argument --meta wt-compiler=webtask-tools/express (or you can use webtask-tools).
Splitting the code into multiple files. Luckily wt-cli provides a bundler so it's just a matter of providing the --bundle argument.
So the final command line becomes:
$ wt create index.js --bundle --meta wt-compiler=webtask-tools/express

Integrating jQuery-File-Upload plugin with blueimp-file-upload-node npm module

I am trying to integrate the jQuery-File-Upload (https://github.com/blueimp/jQuery-File-Upload) with the npm module "blueimp-file-upload-node" to process file uploads.
Sadly, this package "blueimp-file-upload-node" has not been documented yet.
The frontend integration is working correctly, but I am struggling to get the upload functionality working.
I have read and followed this section:
https://github.com/blueimp/jQuery-File-Upload/wiki/Setup#using-jquery-file-upload-ui-version-with-nodejs which tells me to start the service by running:
./node_modules/blueimp-file-upload-node/server.js (notice, the path of this file is within my node_modules folder)
I would like to have the file uploader as a route of my app, (i.e. /upload) not a separate service, on a different port.
How would I go about that?
My code is here:
https://github.com/robsilva/fileUploader
I really appreciate if anyone can shed some light here.
Seems like you are looking for the express middleware
https://github.com/aguidrevitch/jquery-file-upload-middleware

How to log every http.request made in Node

I want log every http request being made by a particular node app, and all of its modules. Wrapping requests in a function could work for all non-module code, the disadvantage would obviously be it doesn't include module code, and be cumbersome to do.
This is for apps already in production, only other option I thought of was tcpdump.
Setting NODE_DEBUG=http will make node log out detailed HTTP request information to the console.
Examples:
NODE_DEBUG=http,http2 node index.js
NODE_DEBUG=http,http2 npm start
For more information see:
NODE_DEBUG documentation
This blog post: Debugging tools and practices in node.js.
As of 2022-11-23, this is the list of the available NODE_DEBUG attributes (based on the blog post above and checking the nodejs source code):
child_process
cluster
esm
fs
http
http2
inspect
module
net
policy
repl
source_map
stream
stream_socket
timer
tls
tracing
worker
You can pass multiple modules as a comma-separated list: NODE_DEBUG=http,http2,tls
How to find the module IDs in nodejs source code:
Unfortunately, there isn't a listing of the available debug module IDs in the nodejs documentation.
To find the IDs in the source: search/grep the nodejs .js files for .debuglog( usage:
# Grep inside the nodejs project
$ grep -r -E --color "\.debuglog\('" nodejs/lib
This will return results like:
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
let debug = require('internal/util/debuglog').debuglog('http2', (fn) => {
The string passed to .debuglog(...) (e.g. 'esm' and 'http2') is the module id that can be passed to NODE_DEBUG.
The easiest / least intrusive way is with a web proxy. Either an off the shelf one or one you write yourself in node. The machines the apps live on would have to get configured* to send all outbound traffic through the proxy and then the proxy can log the traffic. Details on implementation will vary based on which proxy/ approach you pick.
*Arguably, there are ways to do this such that the machines don't even know they're being proxied, but I've found in practice that's really hard to get right, especially with https traffic
In 2022, you can use grackle_tracking library. It helps you track all traffic, errors and analytics into your database or just to the console https://www.getgrackle.com/analytics_and_tracking
Check HTTP request logger called Morgan here

Resources