November helps you generate a simple Node.js API tailored for Ember.js apps, with the help of Express and Sequelize.
$ npm install -g november-cli
$ november new my-app
This will create a new project with the following structure:
├── app
│ ├── actions
│ ├── controllers
│ ├── middleware
│ ├── models
│ ├── router.js
│
├── config
│ ├── config.json
│
├── lib
├── migrations
├── node_modules
├── public
├── server.js
├── test
By default, MySQL is used as a database, but you can use any relational database supported by Sequelize by changing the values in config/config.json
.
To run the app, run npm start
(or just nodemon
if you have it installed) in your app’s directory and visit localhost:9000
.
Make sure you change the host in your Ember.js adapter file so that it can communicate with November:
# In your ember project folder
$ ember generate adapter
// app/adapters/application.js
import DS from "ember-data";
export default DS.RESTAdapter.reopen({
host: 'http://localhost:9000'
});
$ november generate model user
This generates:
app/models/user.js
) for the user, which will determine the structure of the database tableapp/router.js
for creating, reading, updating and deleting users (based on the conventions of Ember Data). Feel free to remove the actions you don't need.app/controllers/user/add.js
app/controllers/user/list.js
app/controllers/user/load.js
app/controllers/user/update.js
app/controllers/user/remove.js
With the app and your local database running in the background, visit localhost:9000/users
, and you should see:
{
"users": []
}
The table users
has automatically been created in your database.
Actions are for API endpoints which are not specifically tied to any model.
$ november generate action login
This generates:
app/actions/login.js
)app/router.js
(POST
by default)The render()
-method in your controllers is used for rendering both your models and your error messages. It takes a single argument.
If you pass a valid sequelize model to render()
, it will generate that model according to the JSON API conventions used by Ember Data.
The most basic usage:
render(<your-sequelize-model>);
...which can also be typed like this:
render({
model: <your-sequelize-model>
});
returns:
{
"user": {
<...>
}
}
If your sequelize model includes associated models, they are sideloaded by default:
{
"user": {
<...>
"tweets": [1, 5]
},
"tweets": [
<...>
]
}
However, you can also specify if you want some (or all) associations to be embedded instead.
Here we specify that we want the tweets-association to be embedded. If we wanted all associations to be embedded, we would set embedded: true
render({
model: <your-sequelize-model>,
embedded: ['tweets']
});
... which returns:
{
"user": {
<...>
"tweets": [
{
id: 1,
<...>
},
{
id: 5,
<...>
}
]
}
}
Controllers generated by November rely heavily on promises. If they catch an error, they call render(error)
.
Let's say we accidentally search for a field (name
) which doesn't exist in our database table:
// app/controllers/user/list.js
req.models.user
.findAll({
where: {
name: "Bob"
}
})
.then(function(users) {
// Not gonna happen
})
.catch(function(err) {
render(err);
});
An error will be catched and render(err)
will return this JSON to the client:
{
"error": {
"code": 500,
"message": "Could not load users"
}
}
... while still showing a more descriptive error to the developer in the console so that you can locate the problem:
You can also render your own exceptions to the user, by throwing a string with the error message or an array where the first element is the error code and the second is the error message:
// app/controllers/user/update.js
req.models.user.find({
where: {
username: req.params.username
}
})
.then(function(user) {
if (user.id !== req.user) {
throw [403, "You are not allowed to edit this user!"]
}
return user.save();
})
.then(function(user) {
// Not gonna happen
})
.catch(function(err) {
render(err);
});
...what the client will see:
{
"error": {
"code": 403,
"message": "You are not allowed to edit this user!"
}
}
TDD is not really my thing, but it would be nice to get some automatic Mocha tests when you generate new models. :)
If you have any questions, feel free to ping me on Twitter or just open an issue!
3 标准化的参考 [注意:这些参考中的许多都在类库的XML描述中被引用。] Extensible Markup Language (XML) 1.0 (Third Edition), 2004 February 4, http://www.w3.org/TR/2004/RECxml-2004 Federal Information Processing Standard (FIPS 1
附录 B. GNU Free Documentation License Version 1.3, 3 November 2008 Copyright © 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc. <http://fsf.org/> Everyone is permitted to copy and distribute
问题内容: 我经常不得不调试编写的Java代码,以便有一个接口和该接口的一个实现。 例如,将有一个接口Foo,其中有一个名为FooImpl的实现。在下面的代码中,如果我按住ctrl键单击doThings,则当我实际要转到FooImpl.java来查看实现时,它将跳至Foo.java。 当我结束界面时,必须使用ctrl-shift-r打开FooImpl。如果我可以按一下ctrl- alt并单击doT
问题内容: 首先,我不是要在这里发动战争。我非常了解Jersey,但是很少使用httpclient。 jersey-client和Apache的httpclient之间的主要区别是什么?在哪些方面比另一方面更好?哪里有比较好的图表?较大的文件(例如2048 MB)中,哪一个效果更好? 非常感谢您的评论! 问题答案: 这两件事可能不应该直接比较。Jersey是REST客户端,具有完整的JAX-RS实
问题内容: 我的应用程序中有两个表。左表就像一个列表。单击左表将打开一个右表,其中包含基于我单击左表的字段的值。 现在的问题是,如果我在右表中编辑一个单元格,而不按Enter键并单击左表中的新项目,则所有表中的特定单元格都处于可编辑模式。以及所有表格的单元格值。 我该如何解决这个问题? 问题答案: 不能完全肯定,我明白你的感受VS你所期望的,但表的默认行为是有点出乎意料的是,当焦点转到表外的某个地
问题内容: 如何使用google-api-java-client解析用户Google日历中事件的开始和结束时间? 从Google代码安装此示例android项目后,我可以进入Google日历并解析一些信息(例如所有日历,事件名称,发布时间和摘要),但是我无法终生获取事件的开始和结束时间。 我对代码的理解是这样的。 在主要活动类(CalendarAndroidSample.java)内,这是获取我每
问题内容: 我正在为小型LibGDX驱动的游戏编写服务器端代码,偶然发现了一个问题。每次尝试使用任何方法时,都会遇到。 显然这是因为我没有实现ApplicationListener,所以LibGDX尚未初始化。 有没有办法以无头/ CLI方式初始化LibGDX?我需要能够在服务器端加载TiledMap对象。 com.esotericsoftware.kryonet.Server $ 1.recei