I am getting this error while working on a nodejs - string

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type function ([Function (anonymous)])
at new NodeError (node:internal/errors:372:5)
at validateString (node:internal/validators:120:11)
at Object.join (node:path:429:7)
at DiskStorage.destination [as getDestination] (E:\create_post_nodejs\routes\postRoute.js:14:23)
at DiskStorage._handleFile (E:\create_post_nodejs\node_modules\multer\storage\disk.js:31:8)
at E:\create_post_nodejs\node_modules\multer\lib\make-middleware.js:137:17
at allowAll (E:\create_post_nodejs\node_modules\multer\index.js:8:3)
at wrappedFileFilter (E:\create_post_nodejs\node_modules\multer\index.js:44:7)
at Multipart.<anonymous> (E:\create_post_nodejs\node_modules\multer\lib\make-middleware.js:107:7) at Multipart.emit (node:events:527:28)
i am creating an api in nodejs for uploading a file/iamge but eventually i am getting the error above

Related

Premature close(Cannot set headers after they are sent to the client) error while streaming video in Nestjs

I'm building a video streaming web server using Nestjs.
I followed the exact same way introduced in nest docs.
However, there are some errors...
MY file.controller.ts
import { Controller, Get, StreamableFile, Response } from '#nestjs/common';
import { createReadStream } from 'fs';
import { join } from 'path';
#Controller('file')
export class FileController {
#Get()
getOne(#Response({ passthrough: true }) res): StreamableFile {
const file = createReadStream(join(process.cwd(), 'sampleVideo.mp4'));
res.set({
'Content-Type': 'video/mp4',
});
return new StreamableFile(file);
}
}
Error code
[Nest] 21224 - 2022. 08. 02. 4:41:16 ERROR [ExpressAdapter] Premature close
Error: Premature close
at new NodeError (node:internal/errors:372:5)
at ServerResponse.onclose (node:internal/streams/end-of-stream:142:30)
at ServerResponse.emit (node:events:539:35)
at Socket.onServerResponseClose (node:_http_server:236:23)
at Socket.emit (node:events:539:35)
at TCP.<anonymous> (node:net:709:12)
Error: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:372:5)
at ServerResponse.setHeader (node:_http_outgoing:576:11)
at ServerResponse.header (C:\Users\whan9\mvp-backend-nestjs\node_modules\express\lib\response.js:794:10)
at ServerResponse.send (C:\Users\whan9\mvp-backend-nestjs\node_modules\express\lib\response.js:174:12)
at StreamableFile.handleError (C:\Users\whan9\mvp-backend-nestjs\node_modules\#nestjs\common\file-stream\streamable-file.js:14:17)
at ReadStream.<anonymous> (C:\Users\whan9\mvp-backend-nestjs\node_modules\#nestjs\platform-express\adapters\express-adapter.js:43:22)
at Object.onceWrapper (node:events:642:26)
at ReadStream.emit (node:events:539:35)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
Environment
Windows 10
node version: v16.16.0
npm: 8.15.1
The sampleVideo.mp4 is OK. It exist in the right place.
If I get rid of res.set({'Content-Type': 'video/mp4'});, the video stream data is downloaded without errors, but also any filename extension.
I want to know what I'm missing...

i am facing with a error :Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client ,anybody hel me

C:\Users\KUMAR\Desktop\DigitelMall\node_modules\mysql\lib\protocol\Parser.js:437
throw err; // Rethrow non-MySQL errors
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
at ServerResponse.header (C:\Users\KUMAR\Desktop\DigitelMall\node_modules\express\lib\response.js:718:10)
at ServerResponse.send (C:\Users\KUMAR\Desktop\DigitelMall\node_modules\express\lib\response.js:163:12)
at ServerResponse.json (C:\Users\KUMAR\Desktop\DigitelMall\node_modules\express\lib\response.js:249:15)
at updateEntity (C:\Users\KUMAR\Desktop\DigitelMall\controllers\MainMenu.js:81:40)
at Query.pool.query (C:\Users\KUMAR\Desktop\DigitelMall\models\CommonModel.js:110:20)
at Query. (C:\Users\KUMAR\Desktop\DigitelMall\node_modules\mysql\lib\Connection.js:526:10)
at Query._callback (C:\Users\KUMAR\Desktop\DigitelMall\node_modules\mysql\lib\Connection.js:488:16)
at Query.Sequence.end (C:\Users\KUMAR\Desktop\DigitelMall\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
You are trying to send the response to a request that you had already responded to.
The response could have been sent by the framework you are using if it implements a default error handler.

Cloud Functions 400 Bad Request with invalid JSON in hello world app

I have hello world cloud functions app, it's working fine either locally by firebase serve or online after firebase deploy
const functions = require('firebase-functions');
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
Locally I can open http://localhost:5000/testing-98b0b/us-central1/helloWorld without any issues, but if I use postman to send post call with an invalid body like "#" it crashes and keep loading with below error:
error: { SyntaxError: Unexpected token # in JSON at position 0
at JSON.parse (<anonymous>)
at createStrictSyntaxError (/usr/local/lib/node_modules/firebase-tools/node_modules/body-parser/lib/types/json.js:158:10)
at parse (/usr/local/lib/node_modules/firebase-tools/node_modules/body-parser/lib/types/json.js:83:15)
at /usr/local/lib/node_modules/firebase-tools/node_modules/body-parser/lib/read.js:121:18
at invokeCallback (/usr/local/lib/node_modules/firebase-tools/node_modules/raw-body/index.js:224:16)
at done (/usr/local/lib/node_modules/firebase-tools/node_modules/raw-body/index.js:213:7)
at IncomingMessage.onEnd (/usr/local/lib/node_modules/firebase-tools/node_modules/raw-body/index.js:273:7)
at IncomingMessage.emit (events.js:182:13)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
expose: true,
statusCode: 400,
status: 400,
body: '#',
type: 'entity.parse.failed' }
Also after firebase deploy I can open
https://us-central1-staff-testing-98b0b.cloudfunctions.net/helloWorld
without any issues but if I use postman to send post call with an invalid body like "#" it crashes and returns 400 bad request with below error:
SyntaxError: Unexpected token # in JSON at position 0
at Object.parse (native)
at createStrictSyntaxError (/var/tmp/worker/node_modules/body-parser/lib/types/json.js:157:10)
at parse (/var/tmp/worker/node_modules/body-parser/lib/types/json.js:83:15)
at /var/tmp/worker/node_modules/body-parser/lib/read.js:121:18
at invokeCallback (/var/tmp/worker/node_modules/raw-body/index.js:224:16)
at done (/var/tmp/worker/node_modules/raw-body/index.js:213:7)
at IncomingMessage.onEnd (/var/tmp/worker/node_modules/raw-body/index.js:273:7)
at emitNone (events.js:86:13)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
Is it bug in the firebase-functions library? Is there any way to handle this type of invalid requests?

Node server is getting down with error buffer.js:488 throw new Error('toString failed');

We have an app build on nodejs (Server A), which is fetching data from another nodejs app(Server B) using REST API's.
Similarly for downloading files Server A make request on Server B and Server B fetches files from AWS S3 and pipes the data back to server A.
This system works perfectly and we are able to download files upto 1GB.
Issue is sometimes Server A crashes with below error:
buffer.js:488
throw new Error('toString failed');
^
Error: toString failed
at Buffer.toString (buffer.js:488:11)
at Request.<anonymous> (/var/www/sky/portal/node_modules/request/request.js:1145:39)
at emitOne (events.js:95:20)
at Request.emit (events.js:182:7)
at IncomingMessage.<anonymous> (/var/www/sky/portal/node_modules/request/request.js:1091:12)
at IncomingMessage.g (events.js:273:16)
at emitNone (events.js:85:20)
at IncomingMessage.emit (events.js:179:7)
at endReadableNT (_stream_readable.js:913:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
Server A is calling server B using below code:
exports.downloadAsset = function(req, res, assetUrl, cb) {
res.cookie('fileDownload', 'true');
request(
config.api.baseUrl + assetUrl
,
function(error, response, body) {
}
).pipe(res);
}
We are not sure from where app is getting crashed.
Please suggest.
The Solution I used to sort above issue:
I had to modify our flow. We were streaming data from Server B to Server A, but now Server B is sending signed URL to Server A and file is being downloaded directly from S3 using that signed url.

loopback.io Error: Can't set headers after they are sent. confirm user

I'm using mocha and supertest to create functional tests. Below is a copy of the test.
it("should verify user POST /api/User/confirm", function (done) {
app.models.Users.findById(userId, function (err, usr) {
json('get', '/api/user/confirm?uid=' + userId + '&token=' + usr.verificationToken)
.expect(204,done);
});
});
However this test returns :
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:689:11)
at ServerResponse.res.setHeader (/node_modules/loopback-explorer/node_modules/express/node_modules/connect/lib/patch.js:134:22)
at ServerResponse.header (/node_modules/loopback/node_modules/express/lib/response.js:700:10)
at ServerResponse.send (/node_modules/loopback/node_modules/express/lib/response.js:154:12)
at ServerResponse.json (/node_modules/loopback/node_modules/express/lib/response.js:240:15)
at ServerResponse.send (/node_modules/loopback/node_modules/express/lib/response.js:142:21)
at defaultHandler (/node_modules/loopback/node_modules/strong-remoting/lib/rest-adapter.js:379:11)
at restErrorHandler (/node_modules/loopback/node_modules/strong-remoting/lib/rest-adapter.js:348:14)
at Layer.handle_error (/node_modules/loopback/node_modules/express/lib/router/layer.js:58:5)
at trim_prefix (/node_modules/loopback/node_modules/express/lib/router/index.js:300:13)
at /node_modules/loopback/node_modules/express/lib/router/index.js:270:7
at Function.proto.process_params (/node_modules/loopback/node_modules/express/lib/router/index.js:321:12)
at next (/node_modules/loopback/node_modules/express/lib/router/index.js:261:10)
at Layer.handle_error (/node_modules/loopback/node_modules/express/lib/router/layer.js:54:12)
at trim_prefix (/node_modules/loopback/node_modules/express/lib/router/index.js:300:13)
at /node_modules/loopback/node_modules/express/lib/router/index.js:270:7
at Function.proto.process_params (/node_modules/loopback/node_modules/express/lib/router/index.js:321:12)
at Object.next (/node_modules/loopback/node_modules/express/lib/router/index.js:261:10)
at Object. (/node_modules/loopback/node_modules/express/lib/router/index.js:603:15)
at Object. (/node_modules/loopback/node_modules/continuation-local-storage/node_modules/async-listener/glue.js:188:31)
at Object.immediate._onImmediate (timers.js:363:16)
at processImmediate as _immediateCallback
I'm using the latest version of loopback at this point. Can anybody point me in the right direction?
This typically results from multiple uses of res.send() in the code. I would check to see if this is the case.

Resources