From f3598b854e96e342b81d0f1ccb2b562ba5fd84f7 Mon Sep 17 00:00:00 2001 From: Tristan Partin Date: Tue, 12 May 2020 20:19:03 -0500 Subject: [PATCH 1/5] fix(status): Send response with 404 when ENOENT is found --- lib/loaders/express.loader.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/loaders/express.loader.ts b/lib/loaders/express.loader.ts index b6eb16b9..75dcd56b 100644 --- a/lib/loaders/express.loader.ts +++ b/lib/loaders/express.loader.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, HttpStatus } from '@nestjs/common'; import { loadPackage } from '@nestjs/common/utils/load-package.util'; import * as fs from 'fs'; import { AbstractHttpAdapter } from '@nestjs/core'; @@ -21,7 +21,7 @@ export class ExpressLoader extends AbstractLoader { const express = loadPackage('express', 'ServeStaticModule', () => require('express') ); - optionsArr.forEach(options => { + optionsArr.forEach((options) => { options.renderPath = options.renderPath || DEFAULT_RENDER_PATH; const clientPath = options.rootPath || DEFAULT_ROOT_PATH; const indexFilePath = this.getIndexFilePath(clientPath); @@ -56,6 +56,14 @@ export class ExpressLoader extends AbstractLoader { app.use(express.static(clientPath, options.serveStaticOptions)); app.get(options.renderPath, renderFn); } + + app.use((err: Error, req, res, next: Function) => { + if (err.message.includes('ENOENT')) { + res.setStatus(HttpStatus.NOT_FOUND); + } + + next(err); + }); }); } } From 321479d0006cee5a8b56777e04db868e99003ea4 Mon Sep 17 00:00:00 2001 From: Tristan Partin Date: Sat, 23 May 2020 22:11:36 -0500 Subject: [PATCH 2/5] fix: check for message being undefined --- lib/loaders/express.loader.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/loaders/express.loader.ts b/lib/loaders/express.loader.ts index 75dcd56b..c463745f 100644 --- a/lib/loaders/express.loader.ts +++ b/lib/loaders/express.loader.ts @@ -58,7 +58,7 @@ export class ExpressLoader extends AbstractLoader { } app.use((err: Error, req, res, next: Function) => { - if (err.message.includes('ENOENT')) { + if (err.message != undefined && err.message.includes('ENOENT')) { res.setStatus(HttpStatus.NOT_FOUND); } From 18e54be69925e6dbbffa554aa1f934da781a7c77 Mon Sep 17 00:00:00 2001 From: Tristan Partin Date: Wed, 24 Jun 2020 09:41:36 -0500 Subject: [PATCH 3/5] Send NotFoundException instead of setting the status code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Micaƫl Mbagira --- lib/loaders/express.loader.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/loaders/express.loader.ts b/lib/loaders/express.loader.ts index c463745f..c89e8724 100644 --- a/lib/loaders/express.loader.ts +++ b/lib/loaders/express.loader.ts @@ -59,7 +59,7 @@ export class ExpressLoader extends AbstractLoader { app.use((err: Error, req, res, next: Function) => { if (err.message != undefined && err.message.includes('ENOENT')) { - res.setStatus(HttpStatus.NOT_FOUND); + return res.send(new NotFoundException(err.message)); } next(err); From 9376e1e483d5c978d1c52e586ab343d5880e80ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mica=C3=ABl=20Mbagira?= Date: Wed, 24 Jun 2020 16:52:38 +0200 Subject: [PATCH 4/5] fixup! Send NotFoundException instead of setting the status code --- lib/loaders/express.loader.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/loaders/express.loader.ts b/lib/loaders/express.loader.ts index c89e8724..bd634f31 100644 --- a/lib/loaders/express.loader.ts +++ b/lib/loaders/express.loader.ts @@ -1,4 +1,4 @@ -import { Injectable, HttpStatus } from '@nestjs/common'; +import { Injectable, NotFoundException } from '@nestjs/common'; import { loadPackage } from '@nestjs/common/utils/load-package.util'; import * as fs from 'fs'; import { AbstractHttpAdapter } from '@nestjs/core'; @@ -58,7 +58,7 @@ export class ExpressLoader extends AbstractLoader { } app.use((err: Error, req, res, next: Function) => { - if (err.message != undefined && err.message.includes('ENOENT')) { + if (err?.message?.includes('ENOENT')) { return res.send(new NotFoundException(err.message)); } From 3687a84b228802e2976bc023773685e6d098b38b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mica=C3=ABl=20Mbagira?= Date: Sun, 30 Aug 2020 22:46:59 +0200 Subject: [PATCH 5/5] fixup! fixup! Send NotFoundException instead of setting the status code --- lib/loaders/express.loader.ts | 15 ++++++------- lib/loaders/fastify.loader.ts | 40 ++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/lib/loaders/express.loader.ts b/lib/loaders/express.loader.ts index bd634f31..6df6c659 100644 --- a/lib/loaders/express.loader.ts +++ b/lib/loaders/express.loader.ts @@ -35,7 +35,12 @@ export class ExpressLoader extends AbstractLoader { const stat = fs.statSync(indexFilePath); options.serveStaticOptions.setHeaders(res, indexFilePath, stat); } - res.sendFile(indexFilePath); + res.sendFile(indexFilePath, null, (err: Error) => { + if (err) { + const error = new NotFoundException(err.message); + res.status(error.getStatus()).send(error.getResponse()); + } + }); } else { next(); } @@ -56,14 +61,6 @@ export class ExpressLoader extends AbstractLoader { app.use(express.static(clientPath, options.serveStaticOptions)); app.get(options.renderPath, renderFn); } - - app.use((err: Error, req, res, next: Function) => { - if (err?.message?.includes('ENOENT')) { - return res.send(new NotFoundException(err.message)); - } - - next(err); - }); }); } } diff --git a/lib/loaders/fastify.loader.ts b/lib/loaders/fastify.loader.ts index 3b234394..39a707d4 100644 --- a/lib/loaders/fastify.loader.ts +++ b/lib/loaders/fastify.loader.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, NotFoundException } from '@nestjs/common'; import { loadPackage } from '@nestjs/common/utils/load-package.util'; import { AbstractHttpAdapter } from '@nestjs/core'; import * as fs from 'fs'; @@ -23,12 +23,31 @@ export class FastifyLoader extends AbstractLoader { () => require('fastify-static') ); - optionsArr.forEach(options => { + optionsArr.forEach((options) => { options.renderPath = options.renderPath || DEFAULT_RENDER_PATH; const clientPath = options.rootPath || DEFAULT_ROOT_PATH; const indexFilePath = this.getIndexFilePath(clientPath); + const renderFn = (req: any, res: any) => { + fs.exists(indexFilePath, (exists: boolean) => { + if (exists) { + const stream = fs.createReadStream(indexFilePath); + if ( + options.serveStaticOptions && + options.serveStaticOptions.setHeaders + ) { + const stat = fs.statSync(indexFilePath); + options.serveStaticOptions.setHeaders(res, indexFilePath, stat); + } + res.type('text/html').send(stream); + } else { + const error = new NotFoundException(); + res.status(error.getStatus()).send(error.getResponse()); + } + }); + }; + if (options.serveRoot) { app.register(fastifyStatic, { root: clientPath, @@ -42,27 +61,14 @@ export class FastifyLoader extends AbstractLoader { ? options.serveRoot + validatePath(options.renderPath as string) : options.serveRoot; - app.get(renderPath, (req: any, res: any) => { - const stream = fs.createReadStream(indexFilePath); - res.type('text/html').send(stream); - }); + app.get(renderPath, renderFn); } else { app.register(fastifyStatic, { root: clientPath, ...(options.serveStaticOptions || {}), wildcard: false }); - app.get(options.renderPath, (req: any, res: any) => { - const stream = fs.createReadStream(indexFilePath); - if ( - options.serveStaticOptions && - options.serveStaticOptions.setHeaders - ) { - const stat = fs.statSync(indexFilePath); - options.serveStaticOptions.setHeaders(res, indexFilePath, stat); - } - res.type('text/html').send(stream); - }); + app.get(options.renderPath, renderFn); } }); }