Layer.match is not a function - node.js

so i work at a startup building a backend web app using the nestJs framework (is slightly based on express) and typeorm for our Db . So we recently ran into an issue where the app literally crashes every time an error is thrown i.e an incorrect password
.the error below :
TypeError: layer.match is not a function
at matchLayer (/home/imuchene/apps/DocuflowApi/node_modules/express/lib/router/index.js:574:18)
at next (/home/imuchene/apps/DocuflowApi/node_modules/express/lib/router/index.js:220:15)
at Function.handle (/home/imuchene/apps/DocuflowApi/node_modules/express/lib/router/index.js:174:3)
at Function.handle (/home/imuchene/apps/DocuflowApi/node_modules/express/lib/application.js:174:10)
at Server.app (/home/imuchene/apps/DocuflowApi/node_modules/express/lib/express.js:39:9)
at Server.emit (events.js:187:15)
at Server.EventEmitter.emit (domain.js:441:20)
at parserOnIncoming (_http_server.js:672:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)
now for context we're using the Error class on javascript, even though we think that this is not the issue and rather the routing mechanism of expressjs crashes after the error is thrown , also its worth noting that our application runs fine locally but once deployed on the vm it causes the issue.

Related

why is my node.js app crashing after write after end error

So I'm trying go through a 8 hours video tutorial on Node.js and Express but I seem to hit a point where I am not understanding well.
There's a quick example of an app running but my app keeps crashing unlike in the videos that seems to be working fine. The app code is below and my dilemma is after as well as the error message on console.
const http = require('http')
const server = http.createServer((req,res)=> {
if(req.url === '/'){
res.end('Welcome to our home page')
}
if(req.url === '/about'){
res.end('Here is our short history')
}
res.end(`
<h1>Oops!</h1>
<p>We can't seem to find the pages you are looking for</p>
Back Home
`)
})
server.listen(5000)
whenever I start the app in my terminal it starts fine. My problem is that when I go to the browser at localhost:5000/, localhost:5000/about my app crashes. If I go to localhost:5000/error* it working fine until I click the link that send me to localhost:5000/ and then crashes again. With the following messages.
node:events:504
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:371:5)
at ServerResponse.end (node:_http_outgoing:846:15)
at Server.<anonymous> (/home/chachoz007/NodeJS/tutorial/app.js:12:6)
at Server.emit (node:events:526:28)
at parserOnIncoming (node:_http_server:951:12)
at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
Emitted 'error' event on ServerResponse instance at:
at emitErrorNt (node:_http_outgoing:726:9)
at processTicksAndRejections (node:internal/process/task_queues:84:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
Now I get that my app crashes because res.end and the end is trying to write after it already ended in one of the if statements from before.
My problem is why it is crashing compared to the tutorial that i am following that is not crashing. My version of node.js is 16.**** compared to the tutorials version of 14.****
i was wondering if someone could give me some intell on this issue.
You can only call .end() once for each request.
Your code executes the .end() method twice whenever the request is for / or /about; once in the if-else and then a second time after that. One way to solve it is to use "else if" and then else for the final .end() call.

Axios Request to Vimeo Forbidden when Trying to Run from Remote Server

I am using Nodejs server and it is deployed on g-cloud. I make a call to the vimeo API by using axios in my server. When I run the server locally, it runs fine. However, it gives a 403 error when the call is made by the deployed server.
The error seems like this:
Error: Request failed with status code 403\n at createError
(/workspace/node_modules/axios/lib/core/createError.js:16:15)\n at settle
(/workspace/node_modules/axios/lib/core/settle.js:17:12)\n at
IncomingMessage.
(/workspace/node_modules/axios/lib/adapters/http.js:312:11)\n at IncomingMessage.emit
(events.js:326:22)\n at endReadableNT (_stream_readable.js:1241:12)\n at
processTicksAndRejections (internal/process/task_queues.js:84:21)
Check if you are getting a 1020 error code.
Vimeo/Cloudflare may be blocking requests from google IP's to prevent bot attacks.
Reference

Node.js sequelize ConnectionManager.getConnection was called after the connection manager was closed

We are using sequelize as ORM in express app. I am getting this error on production environment while failing to reproduce this on local setup. On production, the site is hosted using pm2 and there are 4 workers running for the application. I tried running the same codebase using pm2 on local, but couldn't reproduce this issue.
I am facing this issue in only one of my APIs which processes CSV and saves the record in the database. The error is caused while trying to execute some query while serving the response for the upload API.
The util function when run using another API works fine.
The error traceback
Error here===> Error: ConnectionManager.getConnection was called after the connection manager was closed!
at ConnectionManager.getConnection (/home/ubuntu/unberry-backend/source/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:113:13)
at /home/ubuntu/unberry-backend/source/node_modules/sequelize/lib/sequelize.js:613:111
at /home/ubuntu/unberry-backend/source/node_modules/retry-as-promised/index.js:70:21
at new Promise (<anonymous>)
at retryAsPromised (/home/ubuntu/unberry-backend/source/node_modules/retry-as-promised/index.js:60:10)
at Sequelize.query (/home/ubuntu/unberry-backend/source/node_modules/sequelize/lib/sequelize.js:606:12)
at PostgresQueryInterface.select (/home/ubuntu/unberry-backend/source/node_modules/sequelize/lib/dialects/abstract/query-interface.js:952:33)
at Function.findAll (/home/ubuntu/unberry-backend/source/node_modules/sequelize/lib/model.js:1753:47)
at processTicksAndRejections (internal/process/task_queues.js:89:5)
at async Function.findOne (/home/ubuntu/unberry-backend/source/node_modules/sequelize/lib/model.js:1917:12)
at async sendInviteToUser (/home/ubuntu/unberry-backend/source/src/services/linkService.js:43:22)
at async uploadUser (/home/ubuntu/unberry-backend/source/src/controllers/linkController.js:67:33)
Using multer for file processing. Please share the insights! Thanks in advance.

TypeError: callback is not a function at FSReqWrap.cb

I have created a small application, it is running good but while testing with some load the application use to stop by giving the following error:
TypeError: callback is not a function
at FSReqWrap.cb [as oncomplete] (fs.js:212:19)
I am not finding any specific reason for the above error, my application developed using node express project and it will be listening on the specific port and whenever request come it uses the parameter coming from the URL and based on some logic it sends some text to another server using UDP.
Any help will be appreciated. thanks.

Session storage and mongo connect - Can't Set Headers after they are sent

I've read all the related questions to this one on here, I've searched through Github issues and I've read as much as possible about Express to try and figure this one out. As far is I've read most people say it is something to do with the headers being set after they've been sent usually with res.set() or something similar but I'm not doing that anywhere in my code.
This error only occurs shortly after I've deployed my SailsJS app to Modulus:
Error occurred in session middleware :: 'Error: Error getting collection: sessions\n at /mnt/data/2/node_modules/connect-mongo/lib/connect-mongo.js:160:19\n at Db.collection (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:524:27)\n at MongoStore._get_collection (/mnt/data/2/node_modules/connect-mongo/lib/connect-mongo.js:158:17)\n at MongoStore.set (/mnt/data/2/node_modules/connect-mongo/lib/connect-mongo.js:274:12)\n at Session.save (/mnt/data/2/node_modules/sails/node_modules/express/node_modules/connect/node_modules/express-session/session/session.js:63:25)\n at ServerResponse.end (/mnt/data/2/node_modules/sails/node_modules/express/node_modules/connect/node_modules/express-session/index.js:263:21)\n at ServerResponse.res.end (/mnt/data/2/node_modules/sails/node_modules/express/node_modules/connect/node_modules/compression/index.js:116:15)\n at ServerResponse.res.send (/mnt/data/2/node_modules/sails/node_modules/express/lib/response.js:175:8)\n at ServerResponse.res.json (/mnt/data/2/node_modules/sails/node_modules/express/lib/response.js:223:15)\n at storedUrlCheckServiceDone (/mnt/data/2/api/controllers/ProcessController.js:44:18)\n at tryCatcher (/mnt/data/2/node_modules/bluebird/js/main/util.js:24:31)\n at Promise._settlePromiseFromHandler (/mnt/data/2/node_modules/bluebird/js/main/promise.js:454:31)\n at Promise._settlePromiseAt (/mnt/data/2/node_modules/bluebird/js/main/promise.js:530:18)\n at Promise._settlePromises (/mnt/data/2/node_modules/bluebird/js/main/promise.js:646:14)\n at Async._drainQueue (/mnt/data/2/node_modules/bluebird/js/main/async.js:175:16)\n at Async._drainQueues (/mnt/data/2/node_modules/bluebird/js/main/async.js:185:10)'
http.js:690
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:690:11)
at ServerResponse.res.setHeader (/mnt/data/2/node_modules/sails/node_modules/express/node_modules/connect/lib/patch.js:133:22)
at ServerResponse.res.set.res.header (/mnt/data/2/node_modules/sails/node_modules/express/lib/response.js:577:10)
at ServerResponse.res.send (/mnt/data/2/node_modules/sails/node_modules/express/lib/response.js:142:12)
at Object.<anonymous> (/mnt/data/2/node_modules/sails/lib/hooks/http/middleware/defaults.js:56:24)
at Object.immediate._onImmediate (timers.js:372:16)
at processImmediate [as _immediateCallback] (timers.js:354:15)
[2015-04-13T03:23:03.264Z] Application CRASH detected. Exit code 8.
It's something to do with the sessions and connect-mongo but I have no idea how to figure it out. Usually it happens once or twice straight after deployment but sometimes it'll happen up to 5 or 6 times in a row straight after deployment as well.
I've also seen this error occasionally as well around the same time so it might be related:
/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/base.js:246
throw message;
^
Error: Error setting TTL index on collection : sessions
at /mnt/data/2/node_modules/connect-mongo/lib/connect-mongo.js:169:23
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1497:46
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1630:20
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:170:22
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:714:39
at Cursor.close (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:1009:5)
at commandHandler (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:714:21)
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1903:9
at Server.Base._callHandler (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/base.js:453:41)
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/server.js:488:18
at MongoReply.parseBody (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
at null.<anonymous> (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/server.js:446:20)
at emit (events.js:95:17)
at null.<anonymous> (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:207:13)
at emit (events.js:98:17)
at Socket.<anonymous> (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection.js:440:22)
[2015-04-13T03:24:15.011Z] Application CRASH detected. Exit code 8.
[2015-04-13T03:24:22.048Z] Application restarted with pid 14613
I'm using Node v0.10.37, Sails v0.10.5 and Passport for authentication and just the mongo adapter for session storage.
I'd really appreciate any help or insights you can give as I've spent nearly a week trying to figure this out before posting here and even had Newrelic's support team looking at and they couldn't figure it out either.
Thanks
Update
I'm not seeing this error locally, only on Modulus.
This is a shortened down version of my controller.
I basically have a Auth Service that checks if the users is authenticated, if they are I pass the results off to another promise which checks the db for some of their data, if it find a record then it returns it if not then it calls a 3rd party API and does some processing, once thats done it returns and creates a new record, then returns the results back to the browser. If they aren't authenticated then I send back an error.
There are 4 calls to different Sails services in total in the controller, which all return a promise.
text: function (req, res) {
// Check that the host is authenticated
AuthService.hostname(req).done( authServiceDone, authServiceFail );
function authServiceDone (site) {
// Check the DB for the origin
return urlService.check(req, site.owner).done(UrlCheckServiceDone, UrlCheckServiceFail );
}
function authServiceFail (error) {
return res.json({
error : error
});
}
}

Resources