« Back
read.

How I Structure RESTful APIs using Express 4.

Ruby on Rails had it all, maybe too much of it. By it, I mean the rails magic. But one thing that was really useful is its strict, clear and concise definition and use of its directory structure. This took care of most boilerplate code and all you had to do was create a file in a specific format in your app/models folder. I base my structure on Rails as well and use this as a guideline http://guides.rubyonrails.org/getting_started.html#creating-the-blog-application


Simple structure for Express 4

The following is heavily borrowed from getting started guide on rubyonrails.org and adapted for Express/Node.js

File/Folder Purpose
index.js Entry point for your application. Everything in your app gets triggered through this file.
____________ ________________________________________________________________________
app/ Contains the routes, models, auth, helpers and mailers for your applications. Bulk of the code lives here
____________ ________________________________________________________________________
config/ This folder contains additional configuration files
____________ ________________________________________________________________________
config/environments/ This folder contains environment specific config files such as production.js and development.js
____________ ________________________________________________________________________
config/initializers Initialize all your moving pieces in this folder, such as database.js, redis.js, and server.js
____________ ________________________________________________________________________
package.json This is the file that describes your repo and track your dependencies along with scripts that you can run through npm. Details can be found here
____________ ________________________________________________________________________
lib/ Additional libraries that are utilitarian or helper methods. Like logger.js
____________ ________________________________________________________________________
.env .env file at the root of the project is used as a shortcut way to load environments
____________ ________________________________________________________________________
node_modules/ Extended modules for your application. (Shouldn't be checked into the repo and must let npm manage it through 'npm install'
____________ ________________________________________________________________________
public/ The only folder seen by the world as-is. Contains static files and compiled assets.
____________ ________________________________________________________________________
test/ Unit tests, fixtures, and other test apparatus.

A working example

Here's what I have in my repo.

Directory Structure

Detailed Server Implementation

/index.js

This is the entry point for our server. Only thing that will need modification is async.waterfall which outlines what tasks need to be done. These tasks are contained in `config/initializers/*' files. It is explicitly defined (no magic running of initializers)

config/initializers/server.js

This is what does most of the heavy lifting. server.js is responsible for initialization of the routes and models along with starting the server

app/routes/index.js

Initialization of the routes. Routes are generated from the file name, files need to be named camelCase.js and the route will end up being exposed as /camel-case.

app/routes/users.js

Route that handles all /users calls

app/models/user.js

User model

config/initializers/database.js

Example initializer

.env

Environment file

Source Code

Grab the rest of the code from https://github.com/tolgaek/express-sample-api/

This is just an example, and extraction of certain parts of APIs I built over the past few years.


comments powered by Disqus