LoopBack is an open source API framework built on top of Express optimized for mobile and web. Connect to multiple data sources, write business logic in Node.js, glue on top of your existing services and data, connect using JS, iOS & Android SDKs.
Building a Node.js API backend with LoopBack in 5 Minutes
1. Building an API backend with
LoopBack in 5 Minutes
Raymond Feng
Co-Founder and Architect
StrongLoop, Inc.
2. Why do I need an API
backend?
• Build mobile applications that need to
produce/consume data
– Account for gamers.
• Provision a backend that provides APIs/SDKs
– REST, JS, iOS, Android
• Pick a database
– MongoDB, MySQL, Oracle, PostgreSQL, …
– CRUD and beyond, queries and relations, …
3. Let’s scaffold a Node.js API
server
1.
2.
3.
4.
5.
npm install -g strong-cli (only once)
slc lb project loopback-mysql-example
cd loopback-mysql-example
slc lb datasource mysql --connector mysql
slc lb model account -i --data-source mysql
–
–
–
–
email: string - The email id for the account
level: number - The game level you are in
created: date - The date your account is created
modified: date - The date your account is updated
6. npm install loopback-connector-mysql --save
5. Ready to go!
• Start the server
– node app
– http://localhost:3000/explorer
• Create table in MySQL and add test data
– node create-test-data.js
– http://localhost:3000/api/accounts
– http://localhost:3000/api/accounts/1
• Discover models from MySQL tables
– node discover.js
6. Show me the code
• https://github.com/strongloopcommunity/loopback-mysql-example
7. Switch to MongoDB
• datasources.json
"mongodb": {
"connector": "mongodb",
"host": "server",
"port": 27017,
"database": "demo",
"username": "demo",
"password": "password"
}
• models.json
– Change the dataSource property for Account model to
be “mongodb”
9. LoopBack: Open-source Mobile
Backend Powered by Node.js
Open Source and extensible by design
☁ On-premise or in the cloud
Leverage existing data and services
Dynamically save data from mobile
clients
Push and Geolocation services built-in
Dynamic REST and client APIs for
iOS and Android