diff --git a/src/expressMiddleware.ts b/src/expressMiddleware.ts index 9364fc2..7095acc 100644 --- a/src/expressMiddleware.ts +++ b/src/expressMiddleware.ts @@ -16,6 +16,7 @@ export const expressMiddleware = ( res.setHeader('Allow', 'GET, HEAD'); res.setHeader('Content-Length', '0'); res.end(); + next(); return; } const fpath = parse(req.url as string).pathname; @@ -26,8 +27,15 @@ export const expressMiddleware = ( } const stream = createReadStream(fpath); - stream.on('error', function error(err) { - next(err); + stream.once('error', function error(err) { + // next(err); + if (err.name === 'NoSuchKey') { + (res as any).statusCode = 404; + res.write('Not found'); + res.end(); + } else { + next(err); + } }); stream.pipe(res); }; diff --git a/src/nafs.ts b/src/nafs.ts index 3138179..b7c35cd 100644 --- a/src/nafs.ts +++ b/src/nafs.ts @@ -123,17 +123,12 @@ const activeStorageS3Serve: NAFSFactory = (url) => { path.join(rootPath, fpath).replace(/^\//, ''); const readRemoteStream = (fpath: string) => { - try { - return s3 - .getObject({ - Bucket: bucket, - Key: getS3Path(fpath), - }) - .createReadStream(); - } catch (err) { - console.log('Yes'); - } - return new stream.PassThrough(); + return s3 + .getObject({ + Bucket: bucket, + Key: getS3Path(fpath), + }) + .createReadStream(); }; let cachePath: string | undefined;