on
Italy
- Get link
- X
- Other Apps
npm.slc command line tool using this command npm install -g strongloop. slc command line tool can be used to create applications and other app artifacts.npm install -g loopback.slc command line tool, we would have to install two more npm packages namely: cookie-parser and errorhandler. These packages can be installed using the command: npm install -g cookie-parser, errorhandler.slc which can be used to create new application, model classes, add properties to it, define data sources to name a few.slc loopback.
You will be prompted for name of your application and the folder
(defaults to the name of the application) in which your application
would reside. Below is what you would find on your screens:G:\node>slc loopback _-----_ | | .--------------------------. |--(o)--| | Let's create a LoopBack | `---------´ | application! | ( _´U`_ ) '--------------------------' /___A___\ | ~ | __'.___.'__ ´ ` |° ´ Y ` ? What's the name of your application? loopback-rest ? Enter name of the directory to contain the project: loopback-rest create loopback-rest/ info change the working directory to loopback-rest Generating .yo-rc.json I'm all done. Running npm install for you to install the required dependencies. If this fails, try running the command yourself. create .editorconfig create .jshintignore create .jshintrc create README.md create server\boot\authentication.js create server\boot\explorer.js create server\boot\rest-api.js create server\boot\root.js create server\middleware.json create server\server.js create .gitignore create client\README.md .... npm packages installation output .... Next steps: Change directory to your app $ cd loopback-rest Create a model in your app $ slc loopback:model Compose your API, run, deploy, profile, and monitor it with Arc $ slc arc Run the app $ nodeYou will find the next steps also printed. But for now we have a directory structure created which looks like below:
node .. You can see the below output:G:\node\loopback-rest>node . Browse your REST API at http://localhost:3000/explorer Web server listening at: http://localhost:3000/Opening the URL: http://localhost:3000/explorer in the browser gives us the list of APIs available in the application.
npm install loopback-connector-mysql --save.G:\node\loopback-rest>slc loopback:datasource ? Enter the data-source name: mysql_db ? Select the connector for mysql_db: MySQL (supported by StrongLoop)It requires the data source name and the connector to be used which in this case is MySQL.
server/datasources.json with the following:"mysql_db": {
"name": "mysql_db",
"connector": "mysql"
}
We would have to update the above datasource to define the database name, hostname, port, username and password. Let us update it with the following data:
1
"mysql_db": {
"name": "mysql_db",
"connector": "mysql",
"database":"test",
"host":"localhost",
"port":3306,
"password":"password",
"username":"root"
}
slc loopback:model.
It will ask a series of questions starting with the model name, data
source, model's base class and properties and their types in the model
class. Below is how we provided the answers to the questions and also
the properties:G:\node\loopback-rest>slc loopback:model ? Enter the model name: book ? Select the data-source to attach book to: mysql_db (mysql) ? Select model's base class: PersistedModel ? Expose book via the REST API? Yes ? Custom plural form (used to build REST URL): books Let's add some book properties now. Enter an empty property name when done. ? Property name: name (!) generator#invoke() is deprecated. Use generator#composeWith() - see http://yeoman.io/authoring/composability.html invoke loopback:property ? Property type: string ? Required? Yes Let's add another book property. Enter an empty property name when done. ? Property name: isbn (!) generator#invoke() is deprecated. Use generator#composeWith() - see http://yeoman.io/authoring/composability.html invoke loopback:property ? Property type: string ? Required? Yes Let's add another book property. Enter an empty property name when done. ? Property name: author (!) generator#invoke() is deprecated. Use generator#composeWith() - see http://yeoman.io/authoring/composability.html invoke loopback:property ? Property type: string ? Required? No Let's add another book property. Enter an empty property name when done. ? Property name: pages (!) generator#invoke() is deprecated. Use generator#composeWith() - see http://yeoman.io/authoring/composability.html invoke loopback:property ? Property type: number ? Required? No Let's add another book property. Enter an empty property name when done. ? Property name:Once completed you will find two files generated:
book.js and book.json as shown in the image below:"book": {
"dataSource": "mysql_db",
"public": true
}
When we load the URL http://localhost:3000/explorer/#!/books/ you
will find lot of APIs associated with books. Loading any of the books
APIs, for example this one: http://localhost:3000/api/books will result
in an error stating table test.books was not found. Let us create the table now:CREATE TABLE `test`.`book` ( `id` VARCHAR(250) NOT NULL, `name` VARCHAR(250) NOT NULL , `isbn` VARCHAR(20) NOT NULL , `author` VARCHAR(500) NULL , `pages` INT NULL ) ENGINE = InnoDB;Let us now load the same URL http://localhost:3000/api/books and this time we will get an empty JSON array response.
id
parameter. Let us get the details of the book added above by using the
URL: http://localhost:3000/api/books/123. This gives us the response as
shown in the image below:
Comments
Post a Comment