Follow @endyourif rss Facebook LinkedIn

Node js Tutorial

Node js is a server-side technology that allows you to use the JavaScript programming language, but unlike when you commonly think of JavaScript it is not run on a web site inside a web browser. Node.js as a server can be compared to all other web server technology, such as: IIS, Apache, Nginix, and any others that are not coming to me off the top of my head.

Node js has a very large community and has excellent support for web servers like Express; databases like: Mongodb, Mysql, and event MSSQL.

Node JS Tutorial

Learn Node js by example

Below is a summary of Node js tutorials that will help you step by step become an expert with Node.js. This is a summary of topics I suggest learning where I have a full list describing how to get started with Node js below.

If you are a beginner and looking to get started with Node js tutorials I will describe what is Node js, when to use it, how to install, and a list of examples that I have written that will help you go from beginner to expert.

What is Node js?

  • An asynchronous event-driven written in JavaScript that executes at runtime, similar to PHP that doesn't pre-compile the code like ASP.NET
  • It is designed to build scalable network applications because of its event-driven design
  • The server can handle many concurrent connections and executes a callback to start executing your code; otherwise, Node js will sleep until the next connection is received
  • Node.js is designed with similar concepts to Ruby's Event Machine or Python's Twisted systems. If you are familiar with those languages you can make a smooth transaction from one to the other
  • Even though Node is designed without threads, you can leverage multiple cores of the server by spawning child processes with fork commands

How do I install Node js?

  1. Visit Node.js official site and download the install package based on the operating system where you will be running the technology
  2. If you have the pkg running you can also install it by running: pkg install node
  3. Once Node js is installed, I suggest configuring it to run at start of the server. I suggest running it is as service that runs on start so you don't need to manually start it each time
  4. If you want to run it with one-off commands like: node myscript.js then it would be helpful to set the location to the Node.js executable into your global path variable
  5. Install your common npm packages that you will use in all projects by including the -g or --global parameter with npm install.

How do I get started with Node js?

Below is a list of Node js articles that I have written that contain concrete examples with demos to show you not only how to program it, but what it will output:

Solving No Access-Control-Allow-Origin with Node js and Express

If you are receiving an error similar to XMLHttpRequest cannot load localhost:3000. No Access-Control-Allow-Origin header is present on the requested resource. Origin localhost:8888 is therefore not allowed access. This typically implies that you need to enable CORS on your Node js server that is running Express as the web server. Example code below.

Uncaught ReferenceError: require is not defined

When you're first starting out with Node js, it might be a bit confusing because you are most likely accustomed to writing JavaScript code for the web browser. When Node js is the server, the syntax is similar but there are a few differences around how other JavaScript files and modules are included. When writing on the server side you may include files as follows: require('./mysharedfile');.

Access query string parameters with Express and Node.js

Passing data through the URL is a very common practice. As you can see with the URLs on my website, I pass - what I call - a slug of my post title in the URL. In this case: access-query-string-parameters-with-express-and-node-js. With this data I can figure out dynamically what blog post to show. Another common way is to use query string parameters such as ?id=1. There are two common ways that I perform this using Express and Node.js.

If you are not already familiar with Express you can read my article on setting up Express with Node.js before you dive into this blog.

Can't set headers after they are sent

When you are using Node.js with Express and you've received the dreaded "cannot set headers after they are sent to the client" where do you even begin. This article will explore common causes of this error, such as calling res.writeHead, res.write, or res.redirect.

Updating all npm packages in a Node.js project

I have two common scenarios where I want to update my npm packages. First, my existing project needs a package update or secondly I've started a new project and since I'm lazy I've copied my package.json file from one project to this project. I've found two different ways to perform this.

  1. Changing the versions to * in my packages.json

  2. Using the npm-check-updates package

Let's examine in more details each way.

Passing/reading command line arguments with Node.js

It's quite common when running an application to want to pass arguments to the program to define how it should run when executed. This is no different then with Node.js. To accomplish this I like to use one of two ways:

  1. Reading the array process.argv and manually parsing the variables

  2. Using an npm library such as minimist that helps simplify and (in my opinion) parses them into a nice JavaScript variable

Let's take a look at both ways.

Setup routes in Node.js with Express.js and Jade with controllers, models, and views

I like the structure of Model View Controller (MVC) and I wanted to apply it to my Node.js projects.

I've written previous articles about Node using Jade Templates with Express, but I didn't go into much details about code organization; let's do that now.

Node.js as a background service

When you are developing and testing your application, it's ok to constantly start the Node.js server; however, once you've finished or you are trying to setup a production web server you want the Node.js service to always be running in the background. Let's explore the variety of ways to accomplish this.

Node.js Versus Nginx: Bare Bones Test

This is a quick follow up to yesterday’s post on Node.js Versus Apache2: Bare Bones Test where I performed a straight HTML test of Node.js versus Apache.  With my recent switch to Nginx, I thought it prudent to perform this exact same test swapping out Apache for Nginx.  The results are much more interesting to yesterday’s since there is no total destruction (sorry for the spoiler if you didn’t read yesterday’s article).

Node.js Versus Apache2: Bare Bones Test

I've written a few recent articles on Node.js, mostly to familiarize myself with the effort involved in creating basic applications with it.  I have yet to use it in production, mostly because I’m just not sure how ready I am to use it on a large project…

I have been quite curious with performance compared to the standard tools that I've used forever and ever.  In the following article, I’m going to do some basic comparisons between Node and Apache2 performance.

Node.js – Ajax Pagination without the Pages

With my recent endeavours into Node, I thought that taking a fun article like this one - CakePHP 2-0 Ajax Pagination WITHOUT The Pages – would be a really fun experiment to see how difficult it would be to accomplish in Node.

At the end of the day, the logic is still the exact same, retrieve the items, determine the max length, calculate the number of pages, and then perform AJAX as well scroll down to fill in more content as-needed.

Leveraging the Express API and Jade templates, this is quite painless to implement into Node.

Node using Jade Templates with Express

After my first article on Node - My First Node Application – I setup and installed the Node server as well as the Express add-on package.  In today's article, I'm going to expand on that by creating actual output.  In my research and comparison, Jade templates look quite nice.

Let's be honest, anything that can simplify the mundane typing of HTML code and let me output my content faster is a win-win in my books!

Let's get started.

My First Node Application

I have the unique pleasure of being a technical reviewer for an upcoming book by a former co-worker of mine.  Since it will be his second book on Node (his first being a co-author of Node Up and Running), I thought I should probably get a feel for using Node.  I've of course read some of the documentation and have a thorough understand of JavaScript already, as well as event driven programming – working on some very large MMO projects.

I thought I would share how easy it is to really and truly get up and running with Node.

When should I use Node js?

  • If you've been developing web sites for many years and have mastered JavaScript, you can start using Node because you can continue to use JavaScript and commonize your browser executed code with your server executed code
  • You want to use an event-driven technology that supports an asynchronous I/O architecture
  • Simplifies real-time communication between the web browser and web server without using what is known as "polling" looking for changes to update information on the user's web browser
  • You want a scalable web server that outperforms technologies that have been around for decades like Apache
  • Leverage a huge community of libraries via a package manager to simplify your development

What is npm install?

  • npm install is how you install re-usable packages published by a community of software developers
  • It helps manage the dependencies in your project and, if you want, perform automatic upgrades of Node js packages to keep your libraries/packages up-to-date
  • It is one of the easiest ways to install new packages into your projects including installing dependencies from other packages
  • Through stats like downloads and ratings in helps you use the best packages to solve the problem you are working on
My Books
ASP.NET MVC 5 With Bootstrap and Knockout.js
Knockout.js Building Dynamic Client-Side Applications
20 Recipes for Programming MVC 3
20 Recipes for Programming PhoneGap
Rapid Application Development with CakePHP