ThinkJS is the first Node.js MVC framework that supporting use full ES6/7 features to develop Node.js application. By using async/await
in ES7 or */yield
in ES6, ThinkJS totally resovled the hard problem of asynchronous callbacks nesting hell. It absorbs the design and ideas of lots of other framworks around the world, and makes develop Node.js projects faster and efficient than ever before.
Using ES6/7 features to build projects is very efficent, that must be the trend. The lastest version of Node.js has supported the features of ES6 more friendly, though not all features. At this time, Babel can help us to compile ES6 code to ES5 code.
Babel compile our code to ES5 so we don't need worry about the browser compatibility. So we can resolve the asynchronous callbacks problem by using async/await
or */yield
features.
//user controller, home/controller/user.js
export default class extends think.controller.base {
//login action
async loginAction(self){
//if it's a get request, then display them
if(this.isGet()){
return this.display();
}
//here you can use post() method to get all request data which has checked in logic
let data = this.post();
let md5 = think.md5('think_' + data.pwd);
//take username and encrypted password to match data in database
let result = await this.model('user').where({name: data.name, pwd: md5}).find();
//if no result found, it means username or password error
if(think.isEmpty(result)){
return this.fail('login fail');
}
//write user info into session after reciving user infomation
await this.session('userInfo', result);
return this.success();
}
}
We've used ES6 features like class
, export
, let
and ES7 features like async/await
in this example. Database queries and Session
writing were all asynchronous actions, but here we are writing sync code to handle them with async/await
. Last, it can run in Node.js environment stably after Babel
compiling.
When using ThinkJS, you can apply single module mode, general mode or mutiple modules mode, and to develop projects with it's complexity range from very low to very high.
By default, there are three kinds of project environments: development
, testing
and production
, you can use different configuration in different environment to support different requests. You can also custom and extend them in your projects.
ThinkJS supports MySQL
, MongoDB
and SQLite
. It encapsulates many APIs of the database operations, without having to manually stitching SQL statements. You can automatically prevent SQL injection and other vulnerabilities. It also supports transaction and association and other advanced features.
ThinkJS has a mechanism that could automatically update codes after source files being modified without resort to restart Node.js server and other third party modules.
You can use thinkjs
command to create REST API automatically without writing any extra code. Meanwhile, you can also easily add filter or auth check if you want.
ThinkJS supports some common WebSocket libraries like socket.io
and sockjs
, and packages them to provide the consistent APIs to developers.
ThinkJS includes 1500+ test cases with the code coverage at 95%. Every change has its test case to insure the framework functions well.
Action
in ThinkJS can both response to user request and the CLI invoke. With this feature, we can execute cron job more easily.
ThinkJS supports Hooks and Middlewares, they make the requests handling much more flexible.
ThinkJS builds-in the detailed log function, it makes us read log and track problems easily.
[2015-10-12 14:10:03] [HTTP] GET /favicon.ico 200 5ms
[2015-10-12 14:10:11] [HTTP] GET /zh-cn/doc.html 200 11ms
[2015-10-12 14:10:11] [HTTP] GET /static/css/reset.css 200 3ms
[2015-10-12 14:13:54] [SOCKET] Connect mysql with mysql://root:root@127.0.0.1:3306
[2015-10-12 14:15:32] [Error] Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root3'@'localhost' (using password: YES)
[2015-10-12 14:16:12] [Error] Error: Address already in use, port:8360. http://www.thinkjs.org/doc/error.html#EADDRINUSE
The routers ThinkJS supported include regex router, rule router and static router, and router can be set based on modules. That's very helpful for us to make URLs more simple and reserve their high performance at the same time.
ThinkJS provides us very simple methods to implement i18n and custom themes.
Express and koa are simple frameworks, they all only provide the very basic functions. So for developing complex projects, one must introduces the third party plugins. Though small cores often mean big flexibility, the introducing of other plugins would increases the project's complexity. Besides, no one can ensure all the third party plugins are safety and efficient.
Koa 1.x solved asynchronous callbacks problem by using */yield
feature. But the newer async/await
feature will replace */yield
at last. ThinkJS supports both features well.
On the other hand, ThinkJS choosed to provide the full set of solutions. But not only that, in ThinkJS, every function has been strictly tested for performance optimazition and prevent mermory leaks. And the important thing is that we can use all ES6/7 feature in the project directly.
Sails is another Node.js framework that also provides complete solution. It's convinient because of the encapsulation of databases, REST APIs and security features.
But Sails still uses callbacks in asynchronous code. That's too hard to develop, and can't use ES6/7 feature naturally in the projects.
Even though ThinkJS has many advantages, it has also a few disadvantages too, for example:
You can read more about ES6/7 features here: