Last Updated: Feb. 17, 2016, 9:22 a.m.
Node Package Manager: NPM
sudo apt-get install nodejs npm # On ubuntu
In order to upgrade NPM, once can just use NPM itself:
sudo npm install -g npm
Commonly requred commands:
npm list --global # List globally installed packages
npm list # List locally installed packages
npm uninstall <package> # Uninstall package
npm install <package>@<version> --save # Install particular version of package and saves it to package.json
npm update <package> # Update the package to latest available
npm cache clean # When npm installs a package it keeps a copy, so next time you want to install that package, it doesn't need to hit network. The copies are cached in $HOME/.npm directory. To remove the cached copies use this command
npm init # Creates package.json and helps manage dependencies, entry point, test command, git repository and author details
Every project has a directory structure, conventions and configurations. Setting up or remembering one or two is okay, but remembering and using thousands of them is any developers' nightmare. Yoeman is here for the rescue. It is a scaffolding tool that can quickly help develop start a project. To install Yeoman using NPM:
While in development, we will come across many repetative tasks that we will have to do. These tasks are monotnours and take aways the time, for example, populating fixutes, creating database schemas, initializing environment. Grunt is a rask runner, in order words, explicity used for automation. Grant uses plugins and most of the tasks we want to do can be found and used from plugin listing. Inorder to install grunt, simply type:
npm install -g grunt-cli
Now that we have the hello-world program in Node JS, the next common question is, I am given this large source code and I want to debug and understand better, how do I do it? The answer is, use debugger. Node debugger package enables you to debug the source code in any web-browser. In-order to install Node Debugger globally on your system:
sudo npm install -g node-inspector
and then run the code:
When you make http request to your app, it will automatically kick a debugger window with all the access to source code.
OR Another way is to attach the debugger. First run the node-inspector which will start lisening on port 5858 by default.
Then, open a web-browser with URL: http://127.0.0.1:8080/?port=5858
Then, when you launch you application with debug flag, it will attach to the debugger. For example, in the Sails JS application explained below, the command "sails debug" will attach to debugger and helps us debug the code.
Sails JS is a web MVC framework built on top of Node JS. Installing a Sails JS, starting a projecting and quickly executing is dead simple as mentioned in the below steps:
sudo npm install -g sails
sails new <project_name> && cd <project_name> # Creates a scaffold of Sails
sails lift # Execute the web-project
sails debug # Debug sails web-project
sails generate api <app_name> # Creates a model and a controller in api directory
sails console # Drops you into sails console, lets user play with models, services and configuration, helps try Waterline querying
sails www # Run in production
Sails File Structure:
app.js ---> Starting point of the web-application
api ---> Directory containing the following
controllers ---> All the controller logic
models ---> All the models files that represent a database table, object, document or key-value pair
policies ---> Implement authentication and authorization
config ---> Place where all configuration files are stored
connections.js ---> Contains configurations to connect to different databases
http.js ---> HTTP middleware for every Sails request
models.js ---> Application's default connection to database
policies.js ---> Default policy for all controllers and actions
routes.js ---> Add custom routes to application
sessions.js ---> Setting related to session
tasks ---> Set of grunt tasks
Adapters: A bit of code that provides generic methods to perform database specific operations while encapsulating all the database specific nitigrities.
Blueprints: Are Sails way of quickly generating API routes and actions based on the application design. Blueprint routes and actions together constitute the blueprint API.
The different blueprint actions are: find, findOne, create, update, destroy, populate, add and remove
The different blueprint routes are: RESTful routes, Shortcut routes and Action routes
ORM: Sails uses Waterline as its Object Relational Mapper.
Policies: Simple functions which run before controllers. We can apply one or more policies, or protect its actions individually.
Basic MVC application
In this section, let is aim to develop a basic application to familiarize ourselves with Sails framework. Let us start by createing a new sails application:
sails new test_sails
Now let us generate a controller and a model for people. This generates PeopleController in controller directory and People model in model direcoty.
sails generate api people
Inorder to test the application, bring up the sails project by saying:
and use any REST client to make a GET and POST request to http://localhost:1337/people
Viola... You can store and retrieve any key, values for the 'people'. In my opinion, Sails really has made our development efforts less.
In this section let us explore more about Waterline models and queries. A model is basically defined as below inside a model class:
A simple command line tool to run JS apps continously. All we have to do is install forever:
sudo npm install -g forever
and then run the app using forever:
forever start <app.js>
forever list # List apps being run and managed by forever
forever stop <pid> # Stop apps being run by forever
forever -w start <app> # Starts the app and instantly restarts if any changes made to app
May be, if you get sick of restarting the service during development, use nodemon. It will monitor for any changes in the file system and restart the nodejs service automatically for you. All you have to do is, install and use it:
sudo npm install -g nodemon
Mocha: Test Framework
npm install mocha
Other packages required during testing:
should.js # BDD style assertions
npm install should --save
supertest.js # Library for testing HTTP request response
npm install supertest --save
Then create a directory structure as below:
Edit the file model.test.js with the following content: