How to access and test an internal (non-exports) function in a node.js module?

The rewire module is definitely the answer. Here’s my code for accessing an unexported function and testing it using Mocha. application.js: function logMongoError(){ console.error(‘MongoDB Connection Error. Please make sure that MongoDB is running.’); } test.js: var rewire = require(‘rewire’); var chai = require(‘chai’); var should = chai.should(); var app = rewire(‘../application/application.js’); var logError = app.__get__(‘logMongoError’); … Read more

What is process.env.PORT in Node.js?

In many environments (e.g. Heroku), and as a convention, you can set the environment variable PORT to tell your web server what port to listen on. So process.env.PORT || 3000 means: whatever is in the environment variable PORT, or 3000 if there’s nothing there. So you pass that to app.listen, or to app.set(‘port’, …), and … Read more

In mocha testing while calling asynchronous function how to avoid the timeout Error: timeout of 2000ms exceeded

You can either set the timeout when running your test: mocha –timeout 15000 Or you can set the timeout for each suite or each test programmatically: describe(‘…’, function(){ this.timeout(15000); it(‘…’, function(done){ this.timeout(15000); setTimeout(done, 15000); }); }); For more info see the docs.

Configure Node.js to log to a file instead of the console

You could also just overload the default console.log function: var fs = require(‘fs’); var util = require(‘util’); var log_file = fs.createWriteStream(__dirname + ‘/debug.log’, {flags : ‘w’}); var log_stdout = process.stdout; console.log = function(d) { // log_file.write(util.format(d) + ‘\n’); log_stdout.write(util.format(d) + ‘\n’); }; Above example will log to debug.log and stdout. Edit: See multiparameter version by … Read more

DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server

new Buffer(number) // Old Buffer.alloc(number) // New new Buffer(string) // Old Buffer.from(string) // New new Buffer(string, encoding) // Old Buffer.from(string, encoding) // New new Buffer(…arguments) // Old Buffer.from(…arguments) // New Note that Buffer.alloc() is also faster on the current Node.js versions than new Buffer(size).fill(0), which is what you would otherwise need to ensure zero-filling.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)