"Atwood's Law: any application that can be written in JavaScript will eventually be written in JavaScript." -- Jeff Atwood
This are my notes to quick start an application using StrongLoop.
There's a well documented quick start guide at StrongLoop site here: https://docs.strongloop.com/display/public/LB/Getting+started+with+LoopBack
There's a very nice set of examples in the official StrongLoop github repository:
- https://github.com/strongloop/loopback-getting-started
- https://github.com/strongloop/loopback-getting-started-intermediate
This is a very quick start , or most usefull list of command reference.
Github project: https://github.com/strongloop
Current stable version is 2.10.3
-
Install StrongLoop:
npm install -g strongloop
-
Scaffold App:
slc loopback
-
Add datasource to project:
slc loopback:datasource
-
Create model(s):
slc loopback:model
- Later to add new properties to existing models:
slc loopback:property
- Later to add new properties to existing models:
-
Create relations for models:
slc loopback:relations
-
Run app desenv:
node .
-
Access API: http://localhost:3000/explorer
-
Install upload component storage support:
npm install --save loopback-component-storage
-
Create Datasource for images:
slc loopback:datasource
-
pick
other
option to store images in the file system -
"Enter the connector name" type in
loopback-component-storage
-
The entry in datasouces.json will be like this:
"storage": { "name": "storage", "connector": "loopback-component-storage", "provider": "filesystem", "root": "/home/mmaia/tmp/storage" }
-
-
Install MongoDB connector:
-
Create MongoDB Datasource:slc loopback:Datasource
- pick local mongoDB for development?
LoopBack has built in security which allows to control access to all resources, in order to set control permissions to a model / rest endpoint run:
slc loopback:acl
and follow the prompts.
It's recommended to close all permissions first and then start opening the endpoints that should be available individually.
- Great article direct to the point here: https://strongloop.com/strongblog/how-to-test-an-api-with-node-js/
Mocha testing framework - npm install -g mocha
Supertest - npm install supertest -g
Chai - npm install chai -g
In JS files:
var should = require('chai').should(),
supertest = require('supertest'),
api = supertest('http://localhost:5000');
Check official documentation here.
- Install passport component
npm install loopback-component-passport
- Configure providers: providers.json file
By default strongloop slc generates a file root.js in /server/boot that defines a very simple route which displays the server status. In order to change the configuration so strongloop will load statics and hence you can use this to integrate almost any front end framework out there.
- delete or rename root.js to something without a .js extension.
- change the /server/middleware.json to include in the files section(that is empty by default)
to be like:
"files": {
"loopback#static": {
"params": "$!../client"
}
}
Create any dummy html page in the client folder and access localhost:3000. It's done!
Now, if you want to integrate yeoman generator you'll need to configure some other statics:
"loopback#static": [{
"name": "app",
"paths": ["/app"],
"params": "$!../client/app"
},
{
"name": "bower",
"paths": ["/bower_components"],
"params": "$!../client/bower_components"
},
{
"name": "styles",
"paths": ["/app/styles"],
"params": "$!../client/app/styles"
}]
-
How to format a date in the explorer to insert a record?
- '2014-04-01T18:30:00.000Z'
-
How to format date to use in a filter in the api explorer?
${theModel}.find({ where: { date: {gt: new Date('2014-04-01T18:30:00.000Z')} } });
- ${theModel} - replace with the model name you're querying (reminds me of MongoDB syntax)
- using
Date.now()
also works - using milisseconds to execute calculations also works, i.e -
Date.now - (30 * 24 * 60 * 60 * 1000)
. This value represents one month in milliseconds.
-
How to add download/upload features?
- Use the storage component
- To install
npm install loopback-component-storage --save
By default when you run loopback app with node .
it doesn't print anything to the console, usually during development you want to see some messages regarding each one of the modules used in order to proper "debug" and troubleshoot the application during development. In order to do that you can issue the command to run the server passing in a DEBUG flag with the module name you want to see messages, for instance the following line will enable debug for the DataSources and security:
DEBUG=loopback:datasource,loopback:connector:rest,loopback:security:access-context,loopback:security:acl node .
To get a full list of modules that can be enabled check the official Loopback page for debugging.