Article Directory previously overview
A large lump of code in front of the route, controller, action, attribute are all done, the last part of the function is to string up the call.
Then just say a second middleware, and the last middleware RequestHandler
Registration of RequestHandler Middleware
App.use a little bit and it's over. After the Routehandler has handled the route well, then it is requesthandler Real to call our handler function, which is our action.
import { RequestHandler, RouteHandler } from 'gd-express-basic'//第二个中间件,拦截所有请求对路由做自动映射RouteHandler(_app, controllers);//第三个中间件,处理请求_app.use(RequestHandler);
RequestHandler Request Processing Middleware Code
The
- gets the corresponding action description object from the current request, and if not, go to the back-end middleware, for example, to 404. The
- new controller object and passes the Req,res object in.
- Automatic parsing of completion parameters
- invokes action, resulting in a return result
- determines whether the return result is a view type, if the view type calls render to render the page, or if it is not, returns the object
- Determine if the object that needs to be returned is a jsoncallback call, and yes, it will fit
7.
/** * Request Processing Middleware * * @export * @param {core. Request} req * @param {core. Response} res * @param {(core. nextfunction | Undefined)} Next */export function RequestHandler (req:core. Request, Res:core. Response, Next:core. nextfunction | Undefined) {//1. Get the corresponding action description object from the current request, if not, continue to the back of the middleware, such as go to 404. var desc:actiondescriptor = Res.locals.actionDescriptor if (!desc) {return next && next (); } var CNAME = desc. Controllername; New Promise ((Reslove, reject) = {var CType = desc. Controllertype;//*controller class object//2. New controller object and passes the Req,res object in. var c = new CType (req, res);//new a controller object comes out//3. Automatic parsing of completion parameters var AGRs = Bindactionparameter (desc. Controllertype, Desc. Controllertypename, Desc. ActionType, Desc. ActionName, req)//4. Call action to get the returned result var ActionResult = desc. Actiontype.apply (c, AGRs) return Reslove (ActionResult)}). Then (ActionResult = {if (ActionResult Inst Anceof ViewresuLT) {//5. Determines whether the return result is a view type, if the view type calls render to render the page, and if not, returns the object Promise.resolve (Actionresult.data). Then (Vie Wactionresultdata = {var Findviewnamepath = actionresult.name[0] = = =/'? ACTIONRESULT.NAME.SUBSTR (1) : (CNAME + '/' + actionresult.name) res.render (Findviewnamepath, Viewactionresultdata, (err, html) + = { if (err) {Next && next (err); } else {res.send (HTML); Res.end (); } }); }). catch (function (Viewdataerror) {next && next (Viewdataerror); }); } else if (typeof actionresult!== ' undefined ') {//process object send response JSON//6. Determine which objects need to be returned Is it a jsoncallback call method, and is it appropriate to match let Resultdata = Req.query[' callback ']? Req.query[' callback ' + ' (' + json.stringify (actionresult) + ') ': ActionResult; Res.send (Resultdata); Res.end ()} else {//process not response or Origin response.render or response.send. Process.nexttick ((_res:any) = {if (!_res.finished) {_res.end (); }, Res)}). catch (Processrequesterror = = {Next && next (Processrequesterror); })}
Through the concatenation of the Routehandler and RequestHandler two methods, we make a single call to our entire fragmented function. From the controller's discovery, registration, Action Discovery, registration, action parameter configuration, route parsing, matching, action invocation, processing result adaptation output.
In the process of coding debugging, it is found that some ideas and implementations of dotnet core MVC middleware are basically consistent with express middleware. Sure enough, the thought is the same, hahaha.
"Nodejs" allows Nodejs to process requests like the back-end MVC framework (ASP. NET MVC)--Request Processing Results Adaptation Chapter (7/8)