Develop, Monitor and Scale REST APIs in Node.js

 

Prerequisites

Install

  • Node and npm
  • Mongodb
  • ​strongloop - npm install -g strongloop
  • Register with StrongLoop at http://strongloop.com

Loopback - An Open-source Node.js Framework

Develop APIs from scratch with the open source Loopback.io framework.

Create new app

$ slc loopback

     _-----_
    |       |    .--------------------------.
    |--(o)--|    |  Let's create a LoopBack |
   `---------´   |       application!       |
    ( _´U`_ )    '--------------------------'
    /___A___\    
     |  ~  |     
   __'.___.'__   
 ´   `  |° ´ Y ` 

[?] Enter a directory name where to create the project: (.) 

Connect an API to a datasource

$ cd  myproject 
$ slc loopback:datasource

Install the connector

$ npm install loopback-connector-mongodb --save

Create models

$ slc loopback:model

API Explorer and FrontEnd App Integration

$ slc run
Browse your REST API at http://localhost:3000/explorer
Web server listening at: http://localhost:3000/

API Explorer

Query your data using the mongo CLI

> use test
switched to db test
> show collections;
CoffeeShop
system.indexes
> db.CoffeeShop.find().limit(4)
{ "Name" : "Aesop's Tables", "pos" : { "lat" : 37.5332, "lng" : -85.7302 }, "_id" : ObjectId("543f0787f9fed2bb759fc146") }
{ "Name" : "Award Winners Cafe", "pos" : { "lat" : 41.3896, "lng" : -88.12595 }, "_id" : ObjectId("543f0787f9fed2bb759fc147") }
{ "Name" : "Acadia Cafe", "pos" : { "lat" : 44.454, "lng" : -68.04902 }, "_id" : ObjectId("543f0787f9fed2bb759fc148") }
{ "Name" : "Adams Coffee Shop", "pos" : { "lat" : 42.25639, "lng" : -71.01119 }, "_id" : ObjectId("543f0787f9fed2bb759fc149") }
> 

Model Relations

$ slc loopback:relation

Customer - hasMany Review

  • property name for the relation: reviews
  • custom foreign key: authorId

CoffeeShop - hasMany Review

  • property name for the relation: reviews
  • custom foreign key: coffeeshop

Review - belongsTo Customer

  • property name for the relation: author
  • custom foreign key: authorId

Review - belongsTo CoffeeShop

  • property name for the relation: coffeeshop
  • custom foreign key: coffeeshop

Fine Grained Access Control (ACLs)

$ slc loopback:acl
[?] Select the model to apply the ACL entry to: CoffeeShop
[?] Select the ACL scope: All methods and properties
[?] Select the access type: All (match all types)
[?] Select the role: Any unauthenticated user
[?] Select the permission to apply: Explicitly deny access

LoopBack Studio

$ npm install -g http://get-studio.strongloop.com/strong-studio.tgz

$ cd <yourapp>

$ strong-studio

API Design & Composition

Migration

mysql> use coffee;
Database changed
mysql> show tables;
+------------------+
| Tables_in_coffee |
+------------------+
| Coffee           |
+------------------+
1 row in set (0.15 sec)

mysql> describe Coffee;
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| id        | int(11)      | NO   | PRI | NULL    | auto_increment |
| Name      | varchar(512) | YES  |     | NULL    |                |
| RoastType | varchar(512) | YES  |     | NULL    |                |
+-----------+--------------+------+-----+---------+----------------+
3 rows in set (0.07 sec)

Discovery

Profiling using studio

**Use the loadtest.js script to generate load

Monitoring - On-premises and Hosted

Register and get license key at http://strongloop.com

$ slc run --metrics <statsd-url>

Text

Object Tracking

$ slc run --cluster cpus -d
$ slc runctl objects-start <pid>

Start a cluster

Start object tracking

Start CPU profiling

$ slc runctl cpu-start <pid>

Stop CPU profiling

$ slc runctl cpu-start <pid>
  1. Open .cpuprofile files with Chrome Developer Tools for viewing and analysis.

$ slc runctl heap-snapshot <pid>

Take a heap snapshot

  1. Open .cpuprofile files with Chrome Developer Tools for viewing and analysis.

Hosted monitoring

Title Text

getIndexes()

StrongLoop Controller

Debugging

Run your app in the debugger

$ slc debug

Build and package the application

$ rm -rf node_modules // Shouldnt commit node_modules into master branch  
$ git init .
$ git commit -a -m "Initial commit" 
$ slc build --onto deploy --install --commit

Run StrongLoop Process Manager server


$ slc pm -l 7777

Deploy to Process Manager Server

$ slc deploy http://localhost:7777 deploy  

Run an application cluster

$ slc run --cluster=cpus

Control the cluster using slc runctl

  • View cluster status

  • Change cluster size

  • Restart worker processes

  • ​Stop c

Manage application logs

$ slc run --cluster cpus --log getstarted.%w.log

Thank you!

HTML5- StrongLoop - APIs in minutes with Node.js

By chandrikagole

HTML5- StrongLoop - APIs in minutes with Node.js

  • 1,563