国庆后可能就要使用StrongLoop那套东西来做项目了
原文:http://www.javabeat.net/loopback-mysql/
Loopback是什么?
Loopback是一个开源的Node.js API框架, 使用它你能很方便的创建REST API.
安装Loopback
- 可以直接使用npm安装Loopback
- 首先使用
npm install -g strongloop
安装slc
命令行工具.slc
命令行工具被用来创建. - 接下来使用
npm install -g loopback
安装Loopback. - 在使用slc命令行工具之前,再来安装两个包:
cookie-parser
和errorhandler
.npm install -g cookie-parser, errorhandler
.
创建loopback应用
slc
被用来创建新应用的model, model的属性, 定义数据源等.
使用 slc loopback
创建应用. 运行上面的命令后会提示你输入应用的名字和应用所在的文件夹的名字(默认就是应用的名字).
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
$ node
运行完成后,应用的文件夹结构如下:
自动生成了很多代码,现在先不去管他.
先运行application看看到底自动生成了些什么东西. cd到app目录运行 node .
.
G:\node\loopback-rest>node .
Browse your REST API at http://localhost:3000/explorer
Web server listening at: http://localhost:3000/
在浏览器中打开 http://localhost:3000/explorer 可以看到现在应用有了哪些API.
从上面的图片可以看到关于User的API. 它们都是自动生成的.
在浏览器中打开http://localhost:3000/ :
创建MySQL数据源
首先安装loopback mysql connector: 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)
需要一个数据源的名字, 还要选择一个connector, 在我们的例子中是MySQL.
上面的命令会更新server/datasources.json
:
"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"
}
创建Model
使用 slc loopback:model
创建model. 运行后,要回答一堆问题model name, data source, model的基类 和 model的属性:
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:
完成后会生成两个文件: book.js
和book.json
:
book model会记录在model-config.json 中:
"book": {
"dataSource": "mysql_db",
"public": true
}
打开http://localhost:3000/explorer/#!/books/ 能看到和books有关的. 现在使用这些, 如: http://localhost:3000/api/books 会看到一个报错table test.books was not found. 下面我们先创建表:
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;
测试CRUD APIs
http://localhost:3000/explorer 不止暴露了每个model的API, 同时还提供了测试API的工具.
添加一个Book
返回结果是
获取一个book的详细信息
使用http://localhost:3000/api/books/{id}获取详细信息.
http://localhost:3000/api/books/123:
更新一个book:
结果如下:
删除一个book:
结果如下
现在去获取id为123的book会报错:
获取book集合
通过http://localhost:3000/api/books 获取book集合: