Langages/JavaScript/NodeJS

Un article de Agora2ia.

Image:Nodejs-light.png

Sommaire

Présentation


Commandes

install

  • npm install a-local-module : installer un module en local
  • npm install --save a-local-module : installer le module et l'ajouter dans les dépendances du package.json
  • npm install --save-dev a-module-for-tests : installer le module et l'ajouter dans les dépendances de test du package.json
  • sudo npm install -g a-global-module : installer le module en global

Ma première App

git


Node JS

  • $ npm init (to create package.json)
  • web.js
  • $ npm install express logfmt --save


heroku

Initialization

Getting Started with Node.js on Heroku :

  • $ heroku login
  • Add , "engines": { "node": "0.10.x" } at the end of package.json
  • Create /Procfile with web: node app.js (command should be executed to start a web dyno / HEROKU)
  • $ foreman start (to test)

So, important parts are:

  • Dependencies in package.json
  • Process types in Procfile
  • Application source in app.js



    1. Production

And final steps:

  • `$ git (init |) add | commit`
  • `$ heroku create`
  • (`$ heroku keys:add`)
  • `$ git push heroku master` // *** WRITE yes TO ANSWER THE QUESTION !!! ***

Heroku utils commands:

  • `$ heroku ps`
  • `$ heroku open` // open your app in a browser (url is uggly according to the generated name)
  • `$ heroku ps:scale web=2` // to avoid asleep and latence
  • `$ heroku logs`

Rename App:

https://devcenter.heroku.com/articles/node-best-practices


    1. Staging

https://devcenter.heroku.com/articles/multiple-environments

  • `$ heroku create --remote staging`
  • `$ git push staging master`
  • `$ heroku ps --remote staging`

With following Production:

  • `$ heroku create --remote production`
  • `$ git push production master`
  • `$ heroku ps --remote production`

You’ll need to add contributors, config vars, and add-ons to each individually, for instance.

You may tire of always typing either `--remote staging` or `--remote production` at the end of every command, or cofigure default valuea as follow (with `staging` for exemple):

  • `$ git config heroku.remote staging`


    1.  Integration or other one

Imagine you want to duplicate the `staging` application and use it for `integration` tests. An alternative way to create a staging application is to fork an existing application. The following command will do that:

  • `$ heroku fork -a staging integration`
  • `$ git remote add integration git@heroku.com:integration.git`

Forking an application is very powerful: it copies over the config vars, re-provisions all add-ons, and copies all Heroku Postgres data too.


    1.  Branches
  • `$ git push staging development:master`

This command tells git that you want to push from your local development branch to the master branch of your staging remote.


  1. Web Template

http://www.codeproject.com/Articles/744771/Create-a-Simple-Responsive-website-using-Node-Ex

  • `$ npm install express serve-favicon morgan cookie-parser body-parser debug jade --save`
  • `$ sudo npm install -g express-generator`
  • `$ express -c sed.li`

Warnings:

  • package.json will be erased
    1. Express
    1. Jade
    1. Twitter Bootstrap
  • (npm install twitter-bootstrap --save)
  • (npm install twitter-bootstrap-node --save)
  • Download Twitter Boostrap (http://getbootstrap.com/)
  • Unzip, and copy /dist to MY_PROJECT/public
    1. Font Awesome
 * Examples: [4](http://fortawesome.github.io/Font-Awesome/examples/)

Usage:

  • Download and unzip in `/public` (like `twitter-bootstrap`)
  • Include steelsheat: `<link href="font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" media="screen">`
  • Use `fa` tags in HTML: `

    Authentication

    `


  1. Model & MongoDB

Resources:

    1.  Client


    1. Hosted MongoDB

mongolab.com offers free hosted mongodb service.

      1. REST API

mongolab.com offers REST API to access to your databases, based on your `apiKey`.

To find the `apiKey`:

1. when logged in 1. click on the user (top-right corner):

   * [6](https://mongolab.com/user?username=MY_USERNAME))

1. then the `apiKey` would appear at the bottom of the page.

Then you can discover the content of your database:

Note: You can use the *postman* app for Chrome to look over URLs.


      1. Workflow
  • `$ npm install mongodb --save`
  • `$ heroku addons:add mongolab` // Can be done from the Heroku admin site
  • Create mongo database on mongolab.com : Noter URL, username et password
  • `$ npm install mongoose --save`


    1.  Auth

[10](http://scotch.io/tutorials/javascript/easy-node-authentication-setup-and-local)

  • Complete `package.json`
  • `$ npm install`

Serve Static content

node-static

Node WebKit