lightcore
Convention over Configuration, simplify your code.
For expressjs.
Install
npm install lightcore
Quick start
var core = require('lightcore');
core.app.get('/', function(req, res){
res.send('Hello World!');
});
core.start(); //default port 8964
Libraries
- Based Framework: expressjs
core.app
- Template: swig
core.view
Databases
Currenttly support
- sqlite3 (
core.model.sqlite
) - mysql (
core.model.mysql
) - mongodb(mongoskin) (
core.model.mongodb
) - redis (
core.model.redis
)
Using db by configure it just one time, of cause you must install Modules first.
{
"models": {
"sqlite": "data/rss.db",
"mysql": {
"host": "127.0.0.1"
}
}
}
and now you can use it by
var core = require('lightcore')
core.model.sqlite.get("SELECT 1,2,3", function(err, result){
console.log(result);
});
Configure
- IMPORTANT * Your current work directory is the base directory
put your config.json
under base directory
lightcore will merge your configure,default config under lightcore root directory.
{
"port": 8964,
"express": {
"disable": ["x-powered-by"],
"enable": ["trust proxy"]
},
"static": {
"/statics": "statics",
"/css": "statics/css",
"/js": "statics/js",
"/img": "statics/img"
},
"models": {
"sqlite": "data/rss.db",
"mysql": {
"host": "127.0.0.1"
}
}
}
Configure from Environment Variable
An example
models_mysql=mysql://user:pass@host/db?debug=true&charset=BIG5_CHINESE_CI&timezone=-0700
well, the config will be
{
"models": {
"mysql": "mysql://user:pass@host/db?debug=true&charset=BIG5_CHINESE_CI&timezone=-0700"
}
}
also support
models_mysql_host=127.0.0.1
models_mysql_user=root
the configure is
{
"models": {
"mysql": {
"host": "127.0.0.1",
"user": "root"
}
}
}
items express: auto configure your express js static: your static files,key is request path,value is the directory's path
auto controllers
put js under controllers
directory, and exports a class
an example wrote in coffee script, test.coffee:
class test
get: (req, res)->
res.send 'test'
post: (req, res)->
res.send 'post test'
module.exports.route = {
"/": test,
"/test2": test
}
auto hooks
put js under hooks
directory, and exports a function
an example wrote in coffee script, test.coffee:
push = (arc)->
#TODO: PUSH
console.log 'Push article to remote:',arc.title
exports.hook =
new_article: push
You should fire an event 'new_article'
lightcore.execHook 'new_article', articleObj
lightcore.execHook(event_name, [params])
Licence
BSD
Link
change log
0.1.6: port in environment variable should be UPPER-CASE 0.1.5: Now support automatic read env to config