ngnode
v2.0.7
Published
Simply and Quickly build your MEAN application
Downloads
4
Readme
ngNode
Simply and Quickly build your MEAN application.
Features
- Real time MEAN application.
- Simply and quickly build web application with simple model config object, anything else need to do? no! nothing else!
- Auto generate all things including mongodb collection, CRUD api, datatable, sorting, paging, searching, editing data, etc.
- An User model within it, and with passport.js authentication
Before You Begin, you may want to know:
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.
Usages
dependency:
"ngnode": "2.0.5"
install dependency
npm install
make a config object and call ngNode as function
var 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);
and save as app.js and run
node app.js
guess what? a MEAN application you have built! it is really simple, is it?
How simply ngNode be?
with simple config object above, we can quickly build a MEAN application, and a default model "user" in it.
and you can make your own model as well, once a custom model is created and app start, what ngNode will do with the custom model?
In server side
- mongodb model collection is created with the schema which define in model config
- auto create service with crud actions
- auto create crud routers
- initialize data from initData config when the app start
- custom hooks inject in crud actions
In client side
- custom fields of collection with title, sortable, queryable, edit type.
- auto generate data table with custom page size and pagination
- auto generate "create", "edit" and "remove" link, client side routers and ajax request as well
- display field value in either default value or return form custom "render" function
- multi edit types are support, including simple text, textarea, datepicker, checkbox, date list.
How to define custom model
ngNode will try to find models in your app's "/models" folder
the follow model module is what the custom model config look like:
module.exports = function () {
return {
name: "blog", // model name, would be used as the name in mongodb collection
schema: { // collection's fields schema defined
title: { // field name in mongodb
text: "Title", // display in header of datatable
type: String, // define type in mongoose
editType : "textarea" // [optional] edit type, default is simple text
editable : true, // [optional] can edit? default is true
sortable: true, // [optional] can sort? default is false
render : function(data){ // [optional] custom field's content to display
// data : field's content
// return something you want, or return data with do nothing as default do
}
},
... other fields
}
defSortField: "title", // default sort field
queryFields: ["title"], // fields you want to query when searching
defaultPageSize: 15, // default page size
on: { // hook only in this model, if you want to hook all model, this should define this in application config's "on"
"beforeCreate": function (req, res, model, cb) {
// do something you want before create
cb(model); // callback and continue to create the "model"
cb({err : "something wrong and i don't want to continue to create this 'model'"});
// so avoid the model object has the key "err" when you want to continue the action
}
},
initData : function(mongoose){
// this function will be called when the app is started
}
}
};
A model config which define in node.js module style can use in browser? how?
When the app start, ngNode will pack the custom model module into browser's object by use browserify, it is awesome, is it?
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
Sample
An ngNode's sample can be found in ngNode-sample
- 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 [email protected]/test