NodeJS is an event driven javaScript framework that makes writing asynchronous code a piece of cake! For small apps and a limited user base, this is almost magical – the code can be churned out fast, it is clean and it is perfectly unit testable. The purpose of this blog is not to sell node js though, we’ll instead look at how an application could be built easily.
The applications I work on are primarily client-server, like web apps, or mobile apps with a backend. So here’s what my toolkit with node js looks like
- Express JS: Sinatra like web framework that gives basic structure to the web app
- Sequelize JS: ORM framework
- Require JS: for modularization
- Node DB Migrate module: just like rails db migrations
- Mocha JS: For unit tests
- Chai JS: For test assertions
- Crypto: For encryptions
- Angular JS: For the front end
- MySQL: database
Lets keep the front end out of scope for this article and only focus on getting an app that can serve JSON over REST.
Setting up the environment
- Install nodejs
- Next, we need expressjs
- Run the following12345
npminstall-g express# create the app now. we'll call it 'myapp'express myappcdmyappnode app.js - You should now see something like this “Express server listening on port 3000″
- Yay! You now have a basic Express JS app running!
Configuring the app
- The ExpressJS guide is a pretty good resource for getting started, so I would recommend that you read through it.
- Next, add the following in package.json under dependencies:12345678
"mysql":"*","supervisor":"*","db-migrate":"*","sequelize":"*","requireindex":"*","mocha":"*","crypto":"*","chai":"*" - Save package.json and run “npm install”
- Supervisor module is a great module for development environments and it auto reloads the app on change, so you dont have to restart your node server every time. To run the app using supervisor, just use “supervisor app.js”
- Requireindex is a nice module that helps get all objects from a directory into a single object, without adding a “require” for each file
- Add the error handler in app.js (as described in expressjs guide)1234
app.use(function(err, req, res, next){console.error(err.stack);res.send(500,'Something broke!');}); - Run the app again and access using http://localhost:3000.
Adding database support
- Install mysql
- We already have the node module included in package.json (mysql), so the app is now ready to start talking to the database
- We’ll use node db migrate module to set up the database.
- Create a file database.json under myapp. The contents should look as follows:12345678910111213141516171819
{"dev": {"driver":"mysql","user":"root","database":"myapp"},"test": {"driver":"mysql","user":"root","database":"myapp_test"},"production": {"driver":"mysql","user":"root","database":"myapp"}} - Create a db.js in the myapp directory with the following contents:12345678910111213141516
varexpress = require('express'),Sequelize = require("sequelize");varapp = express();varenv = app.get('env') =='development'?'dev': app.get('env');// db configvarfs = require('fs');vardbConfigFile = __dirname +'/database.json';vardata = fs.readFileSync(dbConfigFile,'utf8');vardbConfig = JSON.parse(data)[env];varpassword = dbConfig.password ? dbConfig.password :null;varsequelize =newSequelize(dbConfig.database, dbConfig.user, password, { logging:true});exports.sequelize = sequelize