ngNode
ngNode是一个可以简单快速开发MEAN web应用的framework
特点
- 实时的MEAN应用,通过配置生成web api,angularjs构建ui界面.
- 通过简单的配置,简单快速创建web应用,除此之外不需要做其他事情
- 自动生成相关功能,包括:mongodb ODM、CRUD api、数据显示datatable、排序、分页、搜索、编辑数据等
- 内置User模块和passport.js验证模块
MEAN相关:
This library runs under MEAN, you can find some infomation about MEAN below. - MongoDB - Go through MongoDB Official Website and proceed to their Official Manual, which should help you understand NoSQL and MongoDB better.
- Express - The best way to understand express is through its Official Website, which has a Getting Started guide, as well as an ExpressJS Guide guide for general express topics. You can also go through this StackOverflow Thread for more resources.
- AngularJS - Angular’s Official Website is a great starting point. You can also use Thinkster Popular Guide, and the Egghead Videos.
- Node.js - Start by going through Node.js Official Website and this StackOverflow Thread, which should get you going with the Node.js platform in no time.
如何使用
dependency:1
"ngnode": "2.0.5"
install dependency1
npm install
创建应用相关配置1
2
3
4
5
6
7
8
9
10
11
12var config = {
appName : "testapp",
mongodConnection : "mongodb://localhost:27017/ngnode",
on : {
"someaction" : function (req, res, model, cb) {
// hook all models here you want when "someaction" execute.
// and can define in the model config as well
// see the model config below for detail
}
}
};
require("ngNode")(config);
保存到app.js然后运行node1
node app.js
猜一下发生了什么?你刚才已经创建了一个MEAN应用!很简单,是吧?
为什么ngNode可以这么简单
通过前面的配置,我们很轻易的就可以创建一个MEAN应用,并且里面已经包含一个user模块
并且你还可以创建自定义模块,那么当创建自定义模块后,ngNode会自动做些什么事情?
服务器端
- 通过自定义字段定义,生成mongodb的collection的配置
- 自动创建crud的服务
- 自动创建crud路由
- app运行是调用initData进行数据初始化
- 注入自定义hooks
浏览器端
- 自定义字段的显示title、是否支持排序、是否可以编辑等
- 自动生成数据表格显示,并且有分页功能
- 自动生成“新建”、“编辑”、“删除”操作链接,并且自动生成ajax调用
- 通过field的render配置,可以自定义显示内容
- 多种编辑模式,包括普通文本、textarea、日期选择、单选框、列表选择
如何创建自定义model
ngNode 会尝试在根目录的“/models”下查找自定义的model
自定义的model配置说明如下:
1 | module.exports = function () { |
为什么我只需要创建一个model的配置,就可以在服务器端和浏览器端使用,怎么做到的?
当app启动时,ngNode使用browserify打包自定义model,并可以在浏览器的web端使用。
dependencies in ngNode
- express : ngNode is a framework of MEAN, so express is included
- mongoose : my first choice mongodb ORM
- passport: I use passport as authentication
- express-session : use express-session to maintain user authentication
- connect-mongo : and use mongodb as session store in express-session
- browserify : model module used in server side and browser side, browserify definitely
- bcrypt : user model use bcrypt to encode password when create user, and compare password when user login
ngNode 示例
- git clone and run
npm install
and runnpm start
- visit in chrome:
http://localhost:9527/user
orhttp://localhost:9527/blog
orhttp://localhost:9527/yourownmodel
when you add “yourownmodel” - login with test@163.com/test