Object built-in object

ObjectRouting

Message handler routing object

The routing object ishttpThe core object of message processing, the server matches according to the routing settingsurland method, and willhttpMessages are forwarded to corresponding processors to complete different transactions.

A simple route can be provided directly as 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.start();

If you need more complex routing customization, you can create a Routing object yourself and handle the routing strategy 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.start();

The routing object matches the message according to the set rules and delivers the message to the first processor that meets the rules. Routing rules added later will be matched first. Creation method:

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

The items matched by the regular expression modify the value attribute of the message, and the sub-items are stored in the params attribute of the message. For example:

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 matched result has no children, value is empty and params is empty. For example:

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, value is empty and params is the first-level sub-item. For example:

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 matched result has only one subkey and no subordinate subkeys, both value and params are this subkey. For example:

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 relationship

Constructor

Routing

Create a message handler routing object

1
new Routing(Object map = {});

Call parameters:

  • map: Object, initialization routing parameters

Create a message handler routing object

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

Call parameters:

  • method: String, specifyhttpRequest method, "*" accepts all methods
  • map: Object, initialization routing parameters

member function

append

Add rules from existing routing objects. After adding, the original routes will be cleared.

1
Routing Routing.append(Routing route);

Call parameters:

  • route: Routing, initialized routing object

Return results:

  • Routing, returns the route object itself

Add a set of routing rules

1
Routing Routing.append(Object map);

Call parameters:

  • map: Object, routing parameters

Return results:

  • Routing, returns the route 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 detailsmq.Handler

Return results:

  • Routing, returns the route object itself

Add a routing rule

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

Call parameters:

  • method: String, specifyhttpRequest 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 detailsmq.Handler

Return results:

  • Routing, returns the route object itself

host

add a grouphttpDomain name routing rules

1
Routing Routing.host(Object map);

Call parameters:

  • map: Object, routing parameters

Return results:

  • Routing, returns the route object itself

Add an accepthttpDomain name routing rules

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 detailsmq.Handler

Return results:

  • Routing, returns the route object itself

all

Add a group to accept allhttpMethod routing rules

1
Routing Routing.all(Object map);

Call parameters:

  • map: Object, routing parameters

Return results:

  • Routing, returns the route object itself

Add a line to accept allhttpMethod routing rules

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 detailsmq.Handler

Return results:

  • Routing, returns the route object itself

get

Add a set of GET method routing rules

1
Routing Routing.get(Object map);

Call parameters:

  • map: Object, routing parameters

Return results:

  • Routing, returns the route object itself

Add an accepthttpGET method routing rules

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 detailsmq.Handler

Return results:

  • Routing, returns the route object itself

post

Add a set of acceptshttpPOST method routing rules

1
Routing Routing.post(Object map);

Call parameters:

  • map: Object, routing parameters

Return results:

  • Routing, returns the route object itself

Add an accepthttpPOST method routing rules

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 detailsmq.Handler

Return results:

  • Routing, returns the route object itself

del

Add a set of acceptshttpDELETE method routing rules

1
Routing Routing.del(Object map);

Call parameters:

  • map: Object, routing parameters

Return results:

  • Routing, returns the route object itself

Add an accepthttpDELETE method routing rules

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 detailsmq.Handler

Return results:

  • Routing, returns the route object itself

put

Add a set of PUT method routing rules

1
Routing Routing.put(Object map);

Call parameters:

  • map: Object, routing parameters

Return results:

  • Routing, returns the route object itself

Add an accepthttpPUT method routing rules

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 detailsmq.Handler

Return results:

  • Routing, returns the route object itself

patch

Add a set of PATCH method routing rules

1
Routing Routing.patch(Object map);

Call parameters:

  • map: Object, routing parameters

Return results:

  • Routing, returns the route object itself

Add an accepthttpPATCH method routing rules

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 detailsmq.Handler

Return results:

  • Routing, returns the route object itself

find

Add a set of FIND method routing rules

1
Routing Routing.find(Object map);

Call parameters:

  • map: Object, routing parameters

Return results:

  • Routing, returns the route object itself

Add an accepthttpFIND method routing rules

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 detailsmq.Handler

Return results:

  • Routing, returns the route object itself

invoke

Process a message or object

1
Handler Routing.invoke(object v) async;

Call parameters:

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

Return results:

  • Handler, return to the next processor

toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String Routing.toString();

Return results:

  • String, returns the string representation of the object

toJSON

Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.

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

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable