Object built-in object

Object Routing

Message processor routing object

The routing object is the core object of http message processing. The server matches the url and method according to the routing settings, and forwards the http message to the corresponding processor to complete different transactions.

A simple route can be provided directly in the form of a JSON object, such as:

1 2 3 4 5 6 7 8
var http = require('http'); var svr = new http.Server(8080, { '/': r => r.response.write('home'), '/help': r => r.response.write('help') }); svr.run();

If you need more complex routing customization, you can create a Routing object yourself and handle routing strategies as needed:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
var http = require('http'); var mq = require('mq'); var app = new mq.Routing(); app.get('/', r => r.response.write('home')); app.get('/help', r => r.response.write('help')); app.post('/help', r => r.response.write('post a help.')); app.get('/home/:user', (r, user) => r.response.write('hello ' + user)); app.get('/user/:id(\\d+)', (r, id) => r.response.write('get ' + id)); app.get('/actions', { '/run': r => r.response.write('running'), '/sleep': r => r.response.write('sleeping'), '(.*)': r => r.response.write('........') }); var svr = new http.Server(8080, app); svr.run();

The routing object matches the message according to the set rules and passes the message to the first processor that meets the rules. The routing rules added later are matched first. How to create:

1 2 3 4
var routing = new mq.Routing({ "^/func1(/.*)$": func1, "^/func2(/.*)$": func2 });

Items matched by regular expressions modify the value attribute of the message, and sub-items are stored in the params attribute of the message. E.g:

1 2 3
var routing = new mq.Routing({ "^/func1(/([0-9]+)/([0-9]+)\.html)$": func1, });

After matching the message "/func1/123/456.html", value == "/123/456.html", params == ["123", "456"];

If the matching result has no children, then value is empty and params is empty. E.g:

1 2 3
var routing = new mq.Routing({ "^/func1/[0-9]+/[0-9]+\.html$": func1, });

After matching the message "/func1/123/456.html", value == "", params == [];

If the matching result has multiple sub-items at the first level, the value is empty and params is the first-level sub-item. E.g:

1 2 3
var routing = new mq.Routing({ "^/func1/([0-9]+)/([0-9]+)\.html$": func1, });

After matching the message "/func1/123/456.html", value == "", params == ["123", "456"];

If the matching result has only one sub-item and no sub-items, both value and params are this sub-item. E.g:

1 2 3
var routing = new mq.Routing({ "^/func1/([0-9]+)/[0-9]+\.html$": func1, });

After matching the message "/func1/123/456.html", value == "123", params == ["123"];

Inheritance

Constructor

Routing

Create a message processor routing object

1
new Routing(Object map = {});

Call parameters:

  • map : Object, initial routing parameters

Create a message processor routing object

1 2
new Routing(String method, Object map);

Call parameters:

  • method : String, specifies the http request method, "*" accepts all methods
  • map : Object, initial routing parameters

Member function

append

Add rules from existing routing objects, the original route will be cleared after adding

1
Routing Routing.append(Routing route);

Call parameters:

  • route : Routing, the initialized routing object

Return result:

  • Routing , returns the routing object itself

Add a set of routing rules

1
Routing Routing.append(Object map);

Call parameters:

  • map : Object, routing parameters

Return result:

  • Routing , returns the routing object itself

Add a routing rule

1 2
Routing Routing.append(String pattern, Handler hdlr);

Call parameters:

  • pattern : String, message matching format
  • hdlr : Handler , built-in message processor, processing function, chain processing array, routing object, see mq.Handler details

Return result:

  • Routing , returns the routing object itself

Add a routing rule

1 2 3
Routing Routing.append(String method, String pattern, Handler hdlr);

Call parameters:

  • method : String, specifies the http request method, "*" accepts all methods, "host" specifies the virtual domain name
  • pattern : String, message matching format
  • hdlr : Handler , built-in message processor, processing function, chain processing array, routing object, see mq.Handler details

Return result:

  • Routing , returns the routing object itself

host

Add a set of routing rules for http domain names

1
Routing Routing.host(Object map);

Call parameters:

  • map : Object, routing parameters

Return result:

  • Routing , returns the routing object itself

Add a routing rule that accepts http domain names

1 2
Routing Routing.host(String pattern, Handler hdlr);

Call parameters:

  • pattern : String, message matching format
  • hdlr : Handler , built-in message processor, processing function, chain processing array, routing object, see mq.Handler details

Return result:

  • Routing , returns the routing object itself

all

Add a set of routing rules that accept all http methods

1
Routing Routing.all(Object map);

Call parameters:

  • map : Object, routing parameters

Return result:

  • Routing , returns the routing object itself

Add a routing rule that accepts all http methods

1 2
Routing Routing.all(String pattern, Handler hdlr);

Call parameters:

  • pattern : String, message matching format
  • hdlr : Handler , built-in message processor, processing function, chain processing array, routing object, see mq.Handler details

Return result:

  • Routing , returns the routing object itself

get

Add a set of GET method routing rules

1
Routing Routing.get(Object map);

Call parameters:

  • map : Object, routing parameters

Return result:

  • Routing , returns the routing object itself

Add a routing rule that accepts the http GET method

1 2
Routing Routing.get(String pattern, Handler hdlr);

Call parameters:

  • pattern : String, message matching format
  • hdlr : Handler , built-in message processor, processing function, chain processing array, routing object, see mq.Handler details

Return result:

  • Routing , returns the routing object itself

post

Add a set of routing rules that accept the http POST method

1
Routing Routing.post(Object map);

Call parameters:

  • map : Object, routing parameters

Return result:

  • Routing , returns the routing object itself

Add a routing rule that accepts the http POST method

1 2
Routing Routing.post(String pattern, Handler hdlr);

Call parameters:

  • pattern : String, message matching format
  • hdlr : Handler , built-in message processor, processing function, chain processing array, routing object, see mq.Handler details

Return result:

  • Routing , returns the routing object itself

del

Add a set of routing rules that accept the http DELETE method

1
Routing Routing.del(Object map);

Call parameters:

  • map : Object, routing parameters

Return result:

  • Routing , returns the routing object itself

Add a routing rule that accepts the http DELETE method

1 2
Routing Routing.del(String pattern, Handler hdlr);

Call parameters:

  • pattern : String, message matching format
  • hdlr : Handler , built-in message processor, processing function, chain processing array, routing object, see mq.Handler details

Return result:

  • Routing , returns the routing object itself

put

Add a set of PUT method routing rules

1
Routing Routing.put(Object map);

Call parameters:

  • map : Object, routing parameters

Return result:

  • Routing , returns the routing object itself

Add a routing rule that accepts the http PUT method

1 2
Routing Routing.put(String pattern, Handler hdlr);

Call parameters:

  • pattern : String, message matching format
  • hdlr : Handler , built-in message processor, processing function, chain processing array, routing object, see mq.Handler details

Return result:

  • Routing , returns the routing object itself

patch

Add a set of PATCH method routing rules

1
Routing Routing.patch(Object map);

Call parameters:

  • map : Object, routing parameters

Return result:

  • Routing , returns the routing object itself

Add a routing rule that accepts http PATCH method

1 2
Routing Routing.patch(String pattern, Handler hdlr);

Call parameters:

  • pattern : String, message matching format
  • hdlr : Handler , built-in message processor, processing function, chain processing array, routing object, see mq.Handler details

Return result:

  • Routing , returns the routing object itself

find

Add a set of FIND method routing rules

1
Routing Routing.find(Object map);

Call parameters:

  • map : Object, routing parameters

Return result:

  • Routing , returns the routing object itself

Add a routing rule that accepts the http FIND method

1 2
Routing Routing.find(String pattern, Handler hdlr);

Call parameters:

  • pattern : String, message matching format
  • hdlr : Handler , built-in message processor, processing function, chain processing array, routing object, see mq.Handler details

Return result:

  • Routing , returns the routing object itself

invoke

Handle a message or object

1
Handler Routing.invoke(object v) async;

Call parameters:

  • v : object , specify the message or object to be processed

Return result:

  • Handler , return to the next processor

toString

Returns the string representation of the object, generally returns "[Native Object]", the object can be reimplemented according to its own characteristics

1
String Routing.toString();

Return result:

  • String , returns a string representation of the object

toJSON

Returns the JSON format representation of the object, generally returns a collection of readable attributes defined by the object

1
Value Routing.toJSON(String key = "");

Call parameters:

  • key : String, unused

Return result:

  • Value , returns a value containing JSON serializable