Complete CRUD App using Express, Node and MongoDB

Category : javascript, node

Date : February 25, 2016

For the last week or so i was desparately looking for angular frontend starter with some sass, gulp and jade integration for standardising the application build. Without any luck i decided to build the application starter kit on my own.

If you have worked with any of the modern day MVC frameworks like Rails or Laravel that come with generators for application scaffolding you will know the ease of getting started without over thinking. More to that, generators enable nearly every application to follow a simillar application structure/pattern. This is why i prefer using express generator as the ground zero.

Assuming that you already have Node and NPM installed in your machine. Let run;

We will be using npm for managing the packages through the project so lets initiate the configuration.

npm init

Fill in whatever you want and let other be the default settings which will create package.json at the current folder.

Next lets add express and express-generator globally.

npm install -g express express-generator

Once express and express generator runs successfully we should get the our application scaffolded. After navigating to your projects folders check if express command is loaded up and runs without giving out any error and create your application.

express crudApp

Create an application with the name crudApp with consoling this.

   create : crudApp
   create : crudApp/package.json
   create : crudApp/app.js
   create : crudApp/public
   create : crudApp/public/images
   create : crudApp/public/stylesheets
   create : crudApp/public/stylesheets/style.css
   create : crudApp/routes
   create : crudApp/routes/index.js
   create : crudApp/routes/users.js
   create : crudApp/public/javascripts
   create : crudApp/views
   create : crudApp/views/index.jade
   create : crudApp/views/layout.jade
   create : crudApp/views/error.jade
   create : crudApp/bin
   create : crudApp/bin/www

   install dependencies:
     $ cd crudApp && npm install

   run the app:
     $ DEBUG=crudApp:* ./bin/www

You are all set. Take a few minutes looking around on how the routing is done and by default it runs on jade as the templating. You can opt-in for other templating engines for plan HTML but once you get to work with Jade a little you will know that its very clean for simple web apps.

It is generally practice to create a npm start script to save some typing at the package.json

  "scripts": {
    "start": "DEBUG=crudApp:* ./bin/www",
    "test": "echo \"Error: no test specified\" && exit 1"

If you have any questions, comments or suggestions, feel free to join the discussion below!