Node.js provide http module to create HTTP service, used to respond to user requests, such as Node.js official website provide examples of creating HTTP services:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
ThinkJS also use http.createServer
to create service, so the whole process contains two parts to start service and respond to user requests.
npm start
or node development.js
.run
method.think.config
think.logger
object.src/bootstrap/master.js
file.src/
.app/
folder.workers
configuration to decide the number of Worker to fork. When the worker process has started, the appReady
event is fired. (Can be captured via think.app.on("appReady")
).think.config
andthink.logger
objects.src/config/extend.js
.think.app.modules
, or empty array if single module.src/controller/*.js
) in the project and place it on thethink.app.controllers
object.src/logic/*.js
) in the project and place it on the think.app.logics
object.src/model/*.js
) and place it on the think.app.models
object.src/service/*.js
) in the project and place it on the think.app.services
object.src/config/router.js
on the think.app.routers
object.src/config/validator.js
on the think.app.validators
object.src/config/middleware.js
and register it with the think.app.use
method.src/config/crontab.js
and registers the timing task service.src/bootstrap/worker.js
startup file.onUncaughtException
andonUnhandledRejection
errors in the process and process them. You can customize these two erroneous handlers by configuring src/config.js
.think.beforeStartServer
registration, where you can register some transactions before starting the service.createServer
, execute this function createServer(port, host, callback)
to create the service.think.app.listen
.think.app.on("appReady")
.think.app.server
object.After the service starts, the following log is printed:
[2017-07-02 13:36:40.646] [INFO] - Server running at http://127.0.0.1:8360
[2017-07-02 13:36:40.649] [INFO] - ThinkJS version: 3.0.0-beta1
[2017-07-02 13:36:40.649] [INFO] - Enviroment: development #current running environment
[2017-07-02 13:36:40.649] [INFO] - Workers: 8 #worker process number
When the user requests service, it will be processed through the following steps.
www/static/
, if a static resource is hit, this middleware will response resource and stop the following middleware.request.body
object for later accessing.ctx.controller
andctx.action
for subsequent processing . If the project is a multi-module structure then there is ctx.module
.ctx
into it. If there is no then skip this.__before
method, if it returnsfalse
, it will not execute all subsequent logic (end prematurely)xxxAction
method exists then it will be executed. If the result isfalse
, then all subsequent logic will not be executedxxxAction
method does not exist, then try the__call
method__after
method, and if it returns false
, no subsequent logic will executethis.body
property and return it to the user.onUncaughtException
or onUnhandledRejection
event, or Worker abnormal exit, Master will capture an error, re-fork a new Worker process, and kill the current process.We can see that all the user requests are handled through middleware. In specific projects, we add assemble more middleware to handle user's request accordingly.