Search
Close this search box.

Beginner Tips for Node.js and Express

Red Argyle Logo without name
Red Argyle logo

Last summer I wrote this blog post about my experience playing with Node.js and connecting it to Salesforce. Since then, Node.js’s popularity and stability have been growing, and I’ve continued to play with it for both personal projects and projects here at Red Argyle. As a result of Node.js’s popularity, there is a massive amount of information on the Internet related to learning Node.js, building an app, all of the extra modules you might need, and so on. Wading through all of this information and pulling out the important and useful bits is no small feat. I encountered a number of hurdles along the way and felt that I should share some of the key things I’ve learned. So this is a brief list of things that I wish I had known at the beginning of my Node.js experience. Much of my list speaks to using Express, which is a lightweight framework for Node.js, but the information applies to other frameworks as well.

Pay Attention to Versions and Dates

There are tons of blog posts and tutorials to help you get up to speed quickly with Node.js and Express, but many of them are now outdated due to the rapid pace of development. For example, the transition from Express version 3 to 4 brought many important changes, and, while migration between versions is possible, it is much easier to start with the most current version and choose a tutorial that uses that version as well. Looking at the publish date on a blog post or tutorial is a good way to judge how current the information is. A tutorial from 2012  probably has lots of outdated information, whereas one published within the past 6-8 months is more likely to use current package versions and development practices.

When it Comes to Template Engines, Skip Jade

Many of the tutorials I came across when I was starting to learn Node.js suggested using the Jade template engine because it is the default template engine for Express. This meant getting up and running easily, but after that I quickly realized the drawback. Jade uses its own shorthand for HTML, which at first glance seems cool, but ultimately means learning new syntax. That takes away from time that could be spent learning Node.js or actually building your app. This custom shorthand also left me feeling “locked in.” After building a couple templates in Jade and then deciding to switch to another engine, it was a pain to translate the Jade shorthand back into HTML. Sticking with an HTML-based template engine means a faster learning curve if you’re already familiar with HTML, easier transition to another engine if necessary, and a more friendly environment in which to collaborate with front-end designers. My personal choice of template engine is Swig, which I find very similar to the Django template language and very easy to implement with Express. This Node Template comparison page is a great resource to help you decide on an engine.

Install Nodemon to Restart Your Application Automatically

When you are actively developing an application, it can get annoying to continuously stop and start the server each time you make changes. Nodemon is a tool that will watch for file changes and restart the Node server automatically. Install Nodemon globally with the command below (Node.js must be installed first) and then use the “nodemon” command to start your server in place of the “node” command. See http://nodemon.io/ for more information.

npm install -g nodemon

Establish an App Structure to Stay Organized

When starting out with a fresh Node.js application you can easily build your app all in one file. This is great for a simple app, but, as you continue to build, that one file will quickly become large and cumbersome. This is why you’ll want to decide on a structure for your app early on in the process. Modularizing your app, or separating it into various files that each focus on a specific task, will really pay off in the future when you want to add functionality and allow quicker onboarding of new developers. I’ve personally found it much easier to track down bugs in a well-organized Node.js app.

Node.js and Express mostly leave it up to the developer to decide how to organize their app, and there are tons of examples out there, but it’s important not to get too hung up on it. Choose a style, work with it, and then be willing to adapt if needed. I tend to separate out my configuration, routes, services, and models into separate folders and files. I find that to be a good starting point that can be adjusted as the app grows. You can also check out this blog post for a good starting structure of a MEAN app (that stands for MongoDB, Express, AngularJS, and Node.js).

Keep these tips in mind as you jump in to the Node.js platform for app building. If you’ve been using Node.js for a while and have other tips you’ve found useful, I’d love to hear your thoughts in the comments below.

Red Argyle logo
Red Argyle logo

Related Blog Posts