Getting Started with Express.js

Intro

Raise your hand if you already know the MVC design pattern ! … oh right you can’t.

You can look at one of my previous blog posts here. Now that we have gotten that out of the way. Express.js provides an incredibly simplistic and transparent way to develop APIs for your applications. It’s one of the most popular frameworks to run on the Node.js platform.

You simply define your routes, define what you want to send out and pull in, and your golden 👍

Setting it up:

Routes:

Here you can see the route in question is just a slash. That means that when ever anyone hits the https://”your domain”/, this route gets triggered (since it is a GET request). Once the route gets triggered it executes the function, that has the “req”, and “res” variables. The “req” is your request object, and the “res” is your response object. You can put different things in the res object and it gets passed along to be rendered in HTML.

This same sort of logic can be applied to POST and PUT requests.

Folder Structures:

You can use a variety of different folder structures. What ever folder structure you use, make sure its suited for the type of app your going to be building. You can checkout an example over here.

And your done ! Easy as that. You may be thinking WTF is that really it ? Why do people want to use Express.js if this is it ? Well the answer is that the frame work does not lock you into, using any specific way of doing things. Therefor you can use any sort of authentication, object relational mapper, session manager, or data base you want.

You can find the best 40 packages most commonly used here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s