This document is from 2.x to 3.x and can not be smoothly upgraded due to the large interface changes in this upgrade. This document is more about the interface change guide.
3.0 abandoned the core architecture of 2.x, built on Koa 2.x and compatible with all the features in Koa. The main changes are:
http
object was changed toctx
objectmiddleware
When the 2.x project starts, all the files in the src/bootstrap/
directory are automatically loaded. 3.0 no longer automatically load all the files, but instead:
src/boostrap/master.js
file in the Master processsrc/boostrap/worker.js
file in the Worker processIf you want to load other files, you can use the require
method in the corresponding file to import it.
2.x will automatically load all files in the src/config/
directory, 3.0 to load the corresponding file according to the function.
Remove hooks and middleware in 2.x, change to middleware in Koa, manage middleware in src/config/middleware.js
config file.
Change the base class think.controller.base
to think.Controller
and remove the think.controller.rest
class.
Change the base class think.model.base
to think.Model
。
Template configuration from the original src/common/config/view.js
migrated to src/config/config.js
, the configuration method is basically the same as before.
The older version of the preRender()
method is deprecated and the new method is named beforeRender()
. nunjucks
template engine parameter order from the original preRender(nunjucks, env, config)
to beforeRender(env, nunjucks, config)
.
Removed methods such as think.prevent
that prevented subsequent execution, replaced with return false in __before
、xxxAction
、__after
to prevent the subsequent code to continue execution.
When 2.x creates a project, a corresponding error.js file is created to handle the error. 3.0 instead use middleware think-trace processing.
Due to 3.0 changes a lot of things, it is not easy to upgrade based on the simple modification of the original project code. It is recommended to create the project with the new scaffolding tool and then copy the previous code one by one into the new project.