A super simple lightweight NodeJS MVC framework(一个超级简单轻量的NodeJS MVC框架)
MIT License
A super simple lightweight NodeJS MVC frameworkNodeJS MVC
requireimportincludeproxyClass
https://github.com/yafoo/jj.js
https://me.i-i.me/special/jj.html
npm i jj.js
node.js >= v12.7.0
1 ./app/controller/index.js
const {Controller} = require('jj.js');
class Index extends Controller
{
async index() {
this.$show('Hello jj.js, hello world !');
}
}
module.exports = Index;
2 ./server.js
const {App, Logger} = require('jj.js');
const app = new App();
app.run(3000, '0.0.0.0', function(err){
!err && Logger.log('app', 'http server is ready on 3000');
});
3
node server.js
4 http://127.0.0.1:3000
Hello jj.js, hello world !
5Stackblitz Hello world !
app //
controller //
index.js //
view //
index //index
index.htm //
middleware //
model //
pagination //
logic //
**** //
app2 //2
common //
config //
app.js //APP
db.js //
routes.js //
view.js //
cache.js //
**** //
public //
static //css image
node_modules //nodejs
server.js //
package.json //npm package.json
./config/app.js``app_multi``true
app2app3./config/app.js``static_dir
const {App, Controller, Db, Model, Pagination, View, Logger, Cookie, Response, Upload, Url, Middleware, Cache, Context, View} = require('jj.js');
Class``Logger``Cache``$
this.$logger
Logger this.$logger.info()``logger``info
hello world
1./app/controller/index.js``user``user``id
1
const {Controller} = require('jj.js');
class Index extends Controller
{
async index() {
this.$show('Hello jj.js, hello world !');
}
async user() {
const user_info = await this.$db.table('user').find({id: 1});
this.$show(user_info);
}
}
module.exports = Index;
urlhttp://127.0.0.1:3000/index/user
json
async user() {}
urlthis.$db
dbController
$``this``$db``./app/``db``$db``this.$db.table
dbtabledb``./``db
jj.jslib``db``lib``db.js``this.$db``db
this.$db``const db = this.$db;``db
Classnewconst db = this.$db; const db1 = new db(this.ctx, options); const db2 = new this.$db(this.ctx, options);
newdb``table()``this.$db.table('user')
dbtable
newdb``db``table``table('user')``find()
ID1
node
this.$xxx
dbnew
ctx
urlurl
2./app/model/user.js
const {Model} = require('jj.js');
class User extends Model
{
async getUserInfo(condition) {
const user_info = await this.db.find(condition);
return user_info;
}
}
module.exports = User;
useruserid1
const {Controller} = require('jj.js');
class Index extends Controller
{
async index() {
this.$show('Hello jj.js, hello world !');
}
async user() {
const user_info = await this.$model.user.getUserInfo({id: 1});
this.$show(user_info);
}
}
module.exports = Index;
urlhttp://127.0.0.1:3000/index/user
json
1this.$model``./app/model/``this.$model.user``./app/model/user.js``getUserInfo``user``getUserInfo
user``this.db``db``$``db``user
1db``$``db
3dbthis.$xxxx
./config/
+./config/xxx.js``./config.js
this.$config.xxx
./config/app.js
const app = {
app_debug: true, //
app_multi: false, //
default_app: 'app', //
default_controller: 'index', //
default_action: 'index', //
common_app: 'common', //
controller_folder: 'controller', //
static_dir: '', // false
koa_body: null // koa-body''nullfalsekoa-bodyuploadpost{multipart: true, formidable: {keepExtensions: true, maxFieldsSize: 10 * 1024 * 1024}}
}
module.exports = app;
./config/db.js
const db = {
default: {
type : 'mysql', //
host : '127.0.0.1', //
database : 'jj', //
user : 'root', //
password : '', //
port : '', //
charset : 'utf8', // utf8
prefix : 'jj_' //
}
}
module.exports = db;
//
url
urlpathnametypeurl
await this.$next();
routes = [
{url: '/', path: 'app/index/index2'}, // '/'appindexindex2
{url: '/article/:id.html', path: 'app/article/article', name: 'article'}, // '/article/123.html'apparticlearticlethis.ctx.params.ididarticlethis.$url.build(':article', {id: 123}); '/article/123.html'
{url: '/admin', path: 'app/admin/check', type: 'middleware'}, // '/admin'appadmincheck
{url: '/about', path: 'app/about/index', type: 'view'}, // '/about'appviewaboutindex.htm
{url: '/:cate/list_:page.html', path: 'cate/cate', name: 'cate_page'}, //
{url: '/hello', path: async (ctx, next) => {ctx.body = 'hello world, hello jj.js!';}} //
];
module.exports = routes;
pathapp
./config/self.js``this.$config.self
const self = {
option1: ''
option2: ''
...
}
module.exports = self;
jj.js/lib/config.js
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}