Node JS Quick Tips

Nodesters check out what we go through daily as we’re working on projects and find useful. 

The tips here will cover NodeJS’s expressive platform.
It’s not just a tool for websites… but can build tools, web apps, desktop applications, and control robots… to name a few. 

Tip #4 Mocha Placeholder Content with FakerJS

When it comes to end-to-end and integration testing it’s important that you ensure that your test are passing, but hard coding data can lead to some serious false positives; that’s where Faker comes in. Faker allows you to randomly generate content, everything from numbers, names, address to images and even credit card numbers. To get started,

npm install faker –save-dev
var faker = require(“faker”),
dateInFureture = faker.date.future(),
randomUserInfo = faker.helpers.userCard();

Tip #3 Promises with Bluebird

Callbacks in nodejs are very beneficial, you can use them as a way to control the flow and logic of your code. However when you start nesting callbacks the depth starts to make code unmanageable and difficult to read. Using a promise library like Bluebird, gives you the ability to chain functions in a clear concise manner.

var Promise = require("bluebird");
function OnDemand(err, doAction){
     return new Promise(resolve, reject){
     //do some action
     if(doAction){
          resolve(doAction.Data); 
     }else{
          reject(throw new DoActionError());
     }
}

Tip #2 Restarting Your Server

Subject: Developing

Although you can start your nodejs server by calling node app.js; you will have to always stop and restart every time you make changes to your server. Bypass that by using nodemon or if you are using gulp you can use  gulp-nodemon

Tip #1 Debug With Time

Subject: Debug

At one point or another you’re going to need to optimize your code, what better way to get started then finding out how long a takes. Using console.time(anyName) and console.timeEnd(anyName) you can log how long it takes for a task to happen in between console.time and console.timeEnd.

If you have any tips you’re looking for, or questions reach out @Passion47 we’ll help however we can 😉