How to use promises in nodejs

This post is going to be about writing promises in nodejs. In many projects that utilize a database server, it’s common that the application needs to wait for the data to be fetched before performing any operations on it. Therefore we need to implement an asynchronous function using promises.


  • Node – v6.11.5


Promise is a built in library in nodejs. It contains two parameters: resolve, reject. Resolve and reject can be named something else but the first parameter represents resolve and second is reject. Promise is always in one of these state: pending,  fulfilled and rejected. Before completing the operation, the promise will always be in the pending state, and you can set its state to either fulfilled or rejected by using the resolve and reject callbacks.

An example of promise may look like this:

var testFunction = () => {
  return new Promise(function(resolve, reject) {
    // Some database operations here
    if (value) {
    } else {

When using this function, you can use .then to use the results of the promise. Here is an example:

  .then(function(result) {
    // Do something with the result data
  }, function(error){
    // Handle error

The result field will have the value returned from resolve(value) and error will have the value returned from reject(value). I have seen a lot of developers either forget or purposefully leave out the error handling part of the promise, but I would recommend to leave it in there, especially if it is used for REST APIs, so you can send res.status(500).send() there.

For people who are still confused about promises, a real life example would be: you are at a fast food place, you are placing your order but you left your wallet in your car. So you made a promise with the cashier that you will go get your wallet from your car and pay when you come back. So the cashier continues to send the order to prepare your food, and when you come back you paid the cashier what you owed. From this example, notice that by making the promise, the cashier was able to continue the process by sending in your order and not block any other processes from operating.

React Nodejs Webpack Boilerplate Project

React is one of the hottest frontend libraries today. Its component based structure makes projects easy to manage. Although it is popular and powerful, it’s still only the view of the MVC framework. Personally I enjoy using Nodejs and Express for backend development. In the past 2 years, I have started many personal and work projects with this stack, so I put together a light boilerplate project to help me kick things off. Now I am not going to go into too much details, but I’ll give a brief explanation about the project structure and what each folder and file is responsible for.

Git repo:


  • React
  • Express
  • Nodejs
  • Webpack
  • Karma

Project Structure






– app

The app folder contains all of the React files as follows





— actions

Actions contains a file called actions.js. It is responsible for managing communications with Redux. You can learn about Redux and what its role in the project through their documentation and some googling, but on a high level, Redux is a state manager for React projects. Unlike regular HTML and Javascript sites, when Redux is updated with newer states, the React component that is connected to the reducer automatically updates its views with the most recent states. Now actions.js contains logic that inserts, removes, or does any magic that you want to manage the reducers.

— components

Components is the core of React projects. Components contains js files that return each view of the project. These components can be included in other components to put together the entire web page, and can also be routed to a path in routes.js

— reducers

As introduced earlier, reducers are the building blocks of Redux. Inside of each reducer, you can initiate different state variables which can be connected to React components to display their values using connect from the react-redux library.

— store

Store allows you to integrate different middleware into your React project. I have seen a lot of developers adding this block of code in app.js, but I find it more organized to have its own location.

— test

This folder is self explanatory, it contains tests for your project.

Getting started

First start by cloning the git repo

 git clone [app_name]

Before doing anything else, we need to install all of the node libraries

 npm install

Now that you have a local copy of this boilerplate project, you need to use webpack to build your React code into vanilla javascript in a generated file /public/bundle.js


At this point, all of the preparations are done, and we can start up our project

 npm start

The project should be started and open your favorite browser and direct to “localhost:8080” to access your web app. If everything’s done with no errors, you should see your boilerplate running successfully.

Please let me know if I left out any important details and if you have any questions.

How to add Comodo PositiveSSL Certificate to a Node/Express server

This blog will teach you how to add a Comodo PositiveSSL Certificate to a Node/Express server.


  • Express – v4.13.4
  • Node – v6.11.5
  • Comodo PositiveSSL
  • Digital Ocean
  • GoDaddy

Before reading further, your environment should have a Node server set up using ExpressJs listening at port 80(HTTP) and port 443(HTTPS) (Learn how to set up a Node/Express server) and upload this server on your Digital Ocean droplet. (Learn how to set up a droplet on Digital Ocean). There are _ steps to applying the Comodo PositiveSSL certificate to the server:

  1. Purchase a domain
  2. Purchase the cert from Comodo
  3. Activate the cert on Digital Ocean
  4. Apply the cert to server

1. Purchase a domain

To purchase a domain is one of the easier steps. Personally, I bought my domain from GoDaddy. You can create an account first then search for the domain or vice versa. After you search for your domain name, you should see something as follows:


Next steps are pretty intuitive so I’m not going to give you a step-by-step. After all the payments are done, you should see your domain under your account and you are done with step one.


Then click DNS to modify your Nameservers to redirect to Digital Oceans:

2. Purchase PositiveSSL Certificate from Comodo

From what I have seen, many people are introducing a free SSL certificate provider called Let’s Encrypt. I’m sure it works well for most but I assume there are people like me who are interested in using Comodo’s SSL certificates. Personally, I purchased the cheapest certificate to play around with, and that is the Positive SSL certificate.

To purchase the Positive SSL certificate, you simply sign up on their website, and click Add to cart next to the Positive SSL row under STANDARD DV SSL CERTIFICATES. After following the instructions and making the payment, you will be asked to provide the website’s domain and method of activation. Activation will be explained in the next section, but just select the CNAME option for now. Next, you will be asked to provide the CSR for the certificate. This can be done using their CSR generation tool, which should be a clickable link in the instructions above the text window. The CSR generation tool should appear in a new tab and it should be a straight forward form filling process. After that is done, store the generated private key at a safe and accessible location from your server, and copy and paste the public key to the previous tab where it originally asked you to provide the CSR.

3. Activate the cert on Digital Ocean

Activating the SSL certificate with Digital Ocean is very simple. At this point, your server droplet should be connected properly and you should be able to visit your site at your domain without a problem. After you purchased the SSL cert, you should received an order summary as follow:

The blurred lines are sensitive information which you should keep only to your self, but you do not need to store them anywhere since you will only need them to activate your SSL certificate. By following the instructions provided above, direct to your Digital Ocean account and click Networking from the top menu. Once the page loads, you can add your domain and it should redirect you to a DNS records management page. Select CNAME to create a record and copy and paste the Alias/Host Name and Point to link in the corresponding fields. Lastly, fill in 3600 for TTL and click Create record to complete the record creation. After 15-20 minutes, refresh your Comodo site and the status should change from pending to active, which means your SSL certificate is ready to be used.

4. Apply the cert to server

This is the final step to making your server secure with a SSL certificate! Once your SSL certificate is activated, you should receive an email from Comodo with all of your certificates (.crt files) in a zip file. I had a hard time transferring these certificates to the server but I was able to do it by using SFTP. Once you transferred these certificated to a safe an accessible folder, open your server.js file and populate the following field that you left blank from before.

var options = {
  key: fs.readFileSync('./ssl/private.key');,
  ca: [
  cert: fs.readFileSync('./ssl/[your_domain_name].cert');

Now try starting your server again go to your domain in the browser and you should see the SSL certificate applied correctly!

Feel free to reach out to me with questions and suggestions!