mime.lookup is not a function in expressjs run inside docker - node.js

I have a docker image running a node process. It is listening to a particular port. When i am trying to access the port using curl or through browser request, I am getting following error -
TypeError: mime.lookup is not a function
at ServerResponse.contentType (/E:/Saurabh/node_modules/express/lib/response.js:592:12)
at ServerResponse.send (/E:/Saurabh/node_modules/express/lib/response.js:145:14)
at app.get (/E:/Saurabh/apis/index.js:26:6)
at Layer.handle [as handle_request] (/E:/Saurabh/node_modules/express/lib/router/layer.js:95:5)
at next (/E:/Saurabh/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/E:/Saurabh/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/E:/Saurabh/node_modules/express/lib/router/layer.js:95:5)
at /E:/Saurabh/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/E:/Saurabh/node_modules/express/lib/router/index.js:335:12)
at next (/E:/Saurabh/node_modules/express/lib/router/index.js:275:10)
If i run same code without docker, then it works as expected.
What can be the issue?

As for npm documentation on mime:
Version 2 is a breaking change from 1.x as the semver implies. Specifically:
lookup() renamed to getType()
If you prefer the legacy version of this module please npm install mime#^1.
Just change the way you call the method to mime.getType(path) or uninstall mime npm uninstall mime and reinstall it as npm install mime#^1.

Related

Spawn memory leak issue

I am not sure how I go about fixing this issue, for some reason, just every now and again my nodeJS app seems to fire a spawn error which I can't figure out how to fix or resolve.
Error: spawn ENOMEM
at ChildProcess.spawn (internal/child_process.js:406:11)
at spawn (child_process.js:553:9)
at Object.execFile (child_process.js:237:17)
at exec (child_process.js:158:25)
at CheckIP (/(PATH)/(APPNAME)/app.js:57:3)
at Layer.handle [as handle_request] (/(PATH)/(APPNAME)/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/(PATH)/(APPNAME)/node_modules/express/lib/router/index.js:317:13)
at /(PATH)/(APPNAME)/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/(PATH)/(APPNAME)/node_modules/express/lib/router/index.js:335:12)
at next (/(PATH)/(APPNAME)/node_modules/express/lib/router/index.js:275:10)
at cookieParser (/(PATH)/(APPNAME)/node_modules/cookie-parser/index.js:71:5)
at Layer.handle [as handle_request] (/(PATH)/(APPNAME)/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/(PATH)/(APPNAME)/node_modules/express/lib/router/index.js:317:13)
at /(PATH)/(APPNAME)/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/(PATH)/(APPNAME)/node_modules/express/lib/router/index.js:335:12)
at next (/(PATH)/(APPNAME)/node_modules/express/lib/router/index.js:275:10)
I am wondering is it possible to have a script that watches for this error and if it happens run:
systemctl stop APPNAME
systemctl start APPNAME
I know that does not fix the problem but until I can work out what the issue is I need a temp solution.
from everything I am reading seems it that the spawn is a memory thing? Not 100% sure, if that is the case could I have a script that releases memory as the app continues to run?Swap
The issue is related to the Memory. More precisely it is linked with Swap Memory.
If you want to automatically restart your node app. You can run it with help of PM2 and configure it's restart strategies
If you are using Ubantu and want to update swap size you can follow instructions mentioned here

My Express/Node.js server keeps hanging up with read ECONNRESET

Backend Setup:
Node.js/Express server running on port 5000
AWS S3 (for images)
AWS RDS Mysql (for data)
Hi guys,
I'm a frontend guy, so I don't know much about backend, I just started learning.
I have a Node.js/Express server on port 5000 that I use to connect to an S3 bucket and an RDS Mysql database, that is enough for the React frontend I'm developing at the moment.
I use nodemon to deal with disconnects and reconnect automatically. It used to work just fine 24/7, but I must have changed something during the last 3 months and now it crashes several times a day and I have to restart the server manually. It's not a huge deal but I would like to learn how to deal with it.
Here's the error I keep getting:
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
Listening on port 5000..
Connection Established Successfully
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:205:27)
--------------------
at Protocol._enqueue (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Connection.query (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/mysql/lib/Connection.js:198:25)
at /Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/routes/meProfileImageData.js:10:12
at Layer.handle [as handle_request] (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/layer.js:95:5)
at next (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/layer.js:95:5)
at /Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/index.js:335:12)
at next (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/index.js:275:10) {
errno: 'ECONNRESET',
code: 'ECONNRESET',
syscall: 'read',
fatal: true
}
Error: Cannot enqueue Query after fatal error.
at Protocol._validateEnqueue (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/mysql/lib/protocol/Protocol.js:212:16)
at Protocol._enqueue (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/mysql/lib/protocol/Protocol.js:138:13)
at Connection.query (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/mysql/lib/Connection.js:198:25)
at /Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/routes/meProfileImageData.js:10:12
at Layer.handle [as handle_request] (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/layer.js:95:5)
at next (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/layer.js:95:5)
at /Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/index.js:335:12) {
code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR',
fatal: false
}
Obviously the second part happens when I try to connect without knowing the server has already crashed.
There are plenty of posts on stackoverflow about similar ECONNRESET problems, so it's very possible that my exact problem has already been replied somewhere else, but to be honest I'm just not good enough at this yet to assimilate the answer by myself, I could use some pointers as to what files the problem seems to be in or whatever you could tell me.
I have seen in several posts that the best way to deal with this kind of problem is to set error listeners, and I'm happy to do just that, but to be honest I don't know where to start because the problem happens when I'm not using the server usually, like at night or when I'm not even in front of the computer. I have no idea how to reproduce the problem on my own, it just happens by itself whenever it happens, so that's why it's so hard for me to know where to start. I looked at the logs on my S3 and RDS Mysql and didn't see anything that seemed directly connected to this.
Could you offer me some pointers based on what you see here? I have some possible ideas:
could it be the router at index.js causing this?
when I look at my RDS Mysql database panel I see at the moment there are 3 active connections, even though I stopped working like 2 hours ago. Could it be that those connections timing out are causing this? (read this in another post)
could it be meProfileImageData.js that should get error listening first?
That's all I have for now, any ideas or pointers are highly welcome, and I'll be glad to run any tests or install any necessary additional software to troubleshoot this if you feel like putting in the time to help me.
Thanks
EDIT 1: had a new error today that seems to point at connections being closed automatically and Node.js not dealing with it before there is an attempt to connect:
Error: read ETIMEDOUT
at TCP.onStreamRead (internal/stream_base_commons.js:205:27)
--------------------
at Protocol._enqueue (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Connection.query (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/mysql/lib/Connection.js:198:25)
at /Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/routes/meProfileImageData.js:10:12
at Layer.handle [as handle_request] (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/layer.js:95:5)
at next (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/layer.js:95:5)
at /Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/index.js:335:12)
at next (/Volumes/Content/Desktop/Dropbox/HugoPrivate/React/ladswork-server/node_modules/express/lib/router/index.js:275:10) {
errno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'read',
fatal: true

Getting error when starting a brand new generator-built app

Greenie here. Trying to start with a new Express project and I already hit a snag. I used the generator to start my app (express --view=handlebars sms_alarm_nodejs) and I got this error:
Error: Failed to lookup view "error" in views directory "c:\Users\joe_t\workspace\sms_alarm_nodejs\views"
application.js:630
at Function.render (c:\Users\joe_t\workspace\sms_alarm_nodejs\node_modules\express\lib\application.js:580:17)
at ServerResponse.render (c:\Users\joe_t\workspace\sms_alarm_nodejs\node_modules\express\lib\response.js:971:7)
at c:\Users\joe_t\workspace\sms_alarm_nodejs\app.js:43:7
at Layer.handle_error (c:\Users\joe_t\workspace\sms_alarm_nodejs\node_modules\express\lib\router\layer.js:71:5)
at trim_prefix (c:\Users\joe_t\workspace\sms_alarm_nodejs\node_modules\express\lib\router\index.js:315:13)
at c:\Users\joe_t\workspace\sms_alarm_nodejs\node_modules\express\lib\router\index.js:284:7
at Function.process_params (c:\Users\joe_t\workspace\sms_alarm_nodejs\node_modules\express\lib\router\index.js:335:12)
at next (c:\Users\joe_t\workspace\sms_alarm_nodejs\node_modules\express\lib\router\index.js:275:10)
at Layer.handle_error (c:\Users\joe_t\workspace\sms_alarm_nodejs\node_modules\express\lib\router\layer.js:67:12)
at trim_prefix (c:\Users\joe_t\workspace\sms_alarm_nodejs\node_modules\express\lib\router\index.js:315:13)
Am I missing a view reference in app.js? I assumed the app generator would handle this for me to start. And can I use handlebars? The sample I saw used pug.
Fixed it. I had to use --view=hbs as the generator parameter.

TypeError: The "digest" argument is required and must not be undefined

I am getting subject error while registering a simple Mean Application through http://localhost:3000/auth/register, that I have simply created using mean init command. pbkdf2 or crypto is generating the error and I have no idea where to look for it.
I have done lots of different things like cleared npm cache, reinstalled using npm etc. Please help me out. Following is some more information.
Mean --version: 0.12.15
npm --version: 5.0.3
node --version: v8.1.0
bower --version: 1.8.0
gulp --version: CLI and Local: 3.9.1
Code for model.UserSchema.methods.hashPassword (D:\Projects\Mean\CampaignBuilder\node_modules\meanio-users\server\models\user.js)
/**
* Hash password
*
* #param {String} password
* #return {String}
* #api public
*/
UserSchema.methods.hashPassword = function(password) {
if (!password || !this.salt) return '';
var salt = new Buffer(this.salt, 'base64');
return crypto.pbkdf2Sync(password, salt, 10000, 64).toString('base64');
};
Register page calls http://localhost:3000/api/register and it generates 500 Internal Server Error.
Firefox is showing following response:
TypeError: The "digest" argument is required and must not be undefined
at pbkdf2 (crypto.js:635:11)
at Object.exports.pbkdf2Sync (crypto.js:628:10)
at model.UserSchema.methods.hashPassword (D:\Projects\Mean\CampaignBuilder\node_modules\meanio-users\server\models\user.js:165:17)
at model.<anonymous> (D:\Projects\Mean\CampaignBuilder\node_modules\meanio-users\server\models\user.js:94:31)
at VirtualType.applySetters (D:\Projects\Mean\CampaignBuilder\node_modules\mongoose\lib\virtualtype.js:94:25)
at model.Document.set (D:\Projects\Mean\CampaignBuilder\node_modules\mongoose\lib\document.js:656:12)
at model._handleIndex (D:\Projects\Mean\CampaignBuilder\node_modules\mongoose\lib\document.js:587:14)
at model.Document.set (D:\Projects\Mean\CampaignBuilder\node_modules\mongoose\lib\document.js:547:24)
at model.Document (D:\Projects\Mean\CampaignBuilder\node_modules\mongoose\lib\document.js:71:12)
at model.Model (D:\Projects\Mean\CampaignBuilder\node_modules\mongoose\lib\model.js:46:12)
at new model (D:\Projects\Mean\CampaignBuilder\node_modules\mongoose\lib\model.js:3621:13)
at create (D:\Projects\Mean\CampaignBuilder\node_modules\meanio-users\server\controllers\users.js:86:24)
at Layer.handle [as handle_request] (D:\Projects\Mean\CampaignBuilder\node_modules\meanio\lib\core_modules\server\node_modules\express\lib\router\layer.js:95:5)
at next (D:\Projects\Mean\CampaignBuilder\node_modules\meanio\lib\core_modules\server\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\Projects\Mean\CampaignBuilder\node_modules\meanio\lib\core_modules\server\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\Projects\Mean\CampaignBuilder\node_modules\meanio\lib\core_modules\server\node_modules\express\lib\router\layer.js:95:5)
at D:\Projects\Mean\CampaignBuilder\node_modules\meanio\lib\core_modules\server\node_modules\express\lib\router\index.js:281:22
at Function.process_params (D:\Projects\Mean\CampaignBuilder\node_modules\meanio\lib\core_modules\server\node_modules\express\lib\router\index.js:335:12)
at next (D:\Projects\Mean\CampaignBuilder\node_modules\meanio\lib\core_modules\server\node_modules\express\lib\router\index.js:275:10)
at loadUser (D:\Projects\Mean\CampaignBuilder\node_modules\meanio-users\server\controllers\users.js:202:24)
at Layer.handle [as handle_request] (D:\Projects\Mean\CampaignBuilder\node_modules\meanio\lib\core_modules\server\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\Projects\Mean\CampaignBuilder\node_modules\meanio\lib\core_modules\server\node_modules\express\lib\router\index.js:317:13)
at D:\Projects\Mean\CampaignBuilder\node_modules\meanio\lib\core_modules\server\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\Projects\Mean\CampaignBuilder\node_modules\meanio\lib\core_modules\server\node_modules\express\lib\router\index.js:335:12)
at next (D:\Projects\Mean\CampaignBuilder\node_modules\meanio\lib\core_modules\server\node_modules\express\lib\router\index.js:275:10)
at D:\Projects\Mean\CampaignBuilder\node_modules\connect-modrewrite\index.js:129:7
at Layer.handle [as handle_request] (D:\Projects\Mean\CampaignBuilder\node_modules\meanio\lib\core_modules\server\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\Projects\Mean\CampaignBuilder\node_modules\meanio\lib\core_modules\server\node_modules\express\lib\router\index.js:317:13)
Per the crypto.pbkdf2Sync method you are missing the final argument which is digest, thus the error being displayed.
crypto.pbkdf2
Also with further investigation you can see that the digest argument was required in Node v6.0.
The previous default seems to have been 'sha1'.
This could also be caused by the wrong version of node / running a version that has calls that are not supported in the version of node that you have active
My Experience: My team has an application that requires Node 6.10.0. Sometimes I forget to switch to this version of node and run the latest code under 8.6.0 and I receive the same error. As soon as I swap back to 6.10.0 the website will allow me to login.
(I can see that the accepted answer also mentions this, but I wanted to add a more obvious answer in case it helps someone out there!)

MEAN.JS: Server username security

Using MEAN.JS is possible to bring a Server Error changing this:
http://localhost:3000/#!/
To:
http://localhost:3000/%C0#!/
Error: Bad Request
at SendStream.error (/home/username/Project/node_modules/express/node_modules/send/index.js:239:16)
at SendStream.pipe (/home/username/Project/node_modules/express/node_modules/send/index.js:411:32)
at serveStatic (/home/username/Project/node_modules/express/node_modules/serve-static/index.js:110:12)
at Layer.handle [as handle_request] (/home/username/Project/node_modules/express/lib/router/layer.js:82:5)
at trim_prefix (/home/username/Project/node_modules/express/lib/router/index.js:269:13)
at /home/username/Project/node_modules/express/lib/router/index.js:236:9
at Function.proto.process_params (/home/username/Project/node_modules/express/lib/router/index.js:311:12)
at /home/username/Project/node_modules/express/lib/router/index.js:227:12
at Function.match_layer (/home/ec2-user/Project/node_modules/express/lib/router/index.js:294:3)
at next (/home/username/Project/node_modules/express/lib/router/index.js:188:10)
We can see the username of the server and this is dangerous...
Any suggestion to solve this bug?
The solution is easy. Only need to modify the message of file app/views/500.server.view.html

Resources