How To Use Routes

Getting Started

You can try the following commands to start svrx routing quickly:

touch route.js # create empty routing file
svrx --route route.js

In your route.js:

get('/blog').to.json({ title: 'svrx' });

Then open /blog, you'll see the json output {title: 'svrx'}.


  • support hot reloading ( check it out by editing your route.js now)
  • easy writing, clear reading
  • support expanding through plugin



For example:


This rule can be translate into:

'to' is just a preposition word here, which can be omitted


Route Methods

svrx route supports all the http methods defined by methods.

⚠️ 'delete' is a reserved word of javascript, so you might use del() to create a DELETE method.

Route Matching

The match rule of svrx route is based on path-to-regexp, which is also used by express and koa.

The following is just some briefs of matching rules, please check path-to-regexp doc for more detail.

Common Rules

  • /svrx/:id: named parameters matching, default parameter rule is (\w+)
  • /svrx/:id(hello|world): named parameters matching with custom parameter matching rule
  • /svrx(.*): unnamed parameters matching
  • /\/svrx\/(.*)$/: use regexp directly for complicated routes


  • named parameters like /:id, can be accessed through
  • unnamed parameters like /(hello|world)/(.*).html, can be accessed through ctx.params[0] and ctx.params[1]
  • regexp like /\/svrx\/(.*)$/, can be accessed through ctx.params[i] in order

Parameters Mapping

In fact, except Action:handle, most actions do not have the ability to access the koa context, so we need parameters mapping for some actions.

Take sendFile as an example:

  • /html/index.html will send ${root}/html/index.html
  • /html/home.htm will send ${root}/html/home.htm

Action List

You can use Route API for Plugins to write your own action.


Send response content.

get('/blog').to.send({ title: 'this is a blog' });

send is a syntactic sugar for ctx.body of koa. And there're some default behaviors for different payload types.

  • string
    • if started with <, like <html>, the Content-Type header will be set as text/html
    • if not, return text/plain
  • object or array or number or boolean ...
    • return json, the Content-Type will be application/json


Send file content, and it will auto set the Content-Type header according to the file extension.

  • root path = serve.base || root
  • ⚠️support parameters mapping, for example:


Send json response, despite the type of payload.

get('/blog').to.json({title: 'svrx'});

redirect(target[, code])

Server side redirecting.

  • target: target path
  • code: http code, default is 302

⚠️support parameters mapping, for example:


Set response headers. header doesn't send any response content, so you can chain this action to other actions.

  .to.header({ 'X-Engine': 'svrx' })
  .json({ code: 200 });


Rewrite routes.

⚠️support parameters mapping

get('/svrx(.*)').to.send('Hello svrx')

Both /old/1 and /svrx/1 will return Hello svrx.

rewrite doesn't send any response content, you can chain this action to other actions.

proxy(target[, options])

Proxy path to target server.

  • target: target server
  • options: same as proxy.options
    • changeOrigin
    • secure
    • pathRewrite
get('/test(.*)').to.proxy('', {
  secure: false,


handle is a powerful action, it defines a middleware of koa, which means all actions above can be implemented by handle, but the cost is the reduction of code readability.

  ctx.type = 'html'
  ctx.body = '<body>Hello World</body>'

Instead of using handle, it is recommended to use 'smaller' actions, you can customize your own actions using route api for plugins, see next section.

Route API for Plugins

You can create a new action in your own plugin. There's a router object in your hooks.onCreate, which has 3 methods inside:

  • action: register an action just like proxy, json, ...
  • load: load a routing file
  • route: define a router in scripts

Please read How To Write A Plugin for more information.


get('/handle(.*)').to.handle((ctx) => { ctx.body = 'handle'; });
get('/blog(.*)').to.json({ code: 200 });
get('/code(.*)').to.send('code', 201);
get('/json(.*)').to.send({ json: true });
get('/test(.*)').to.proxy('', {
  secure: false,
get('/query(.*)').to.handle((ctx) => {
  ctx.body = ctx.query;
  .to.header({ 'X-From': 'svrx' })
  .json({ user: 'svrx' });
get('/user').to.json({ user: 'svrx' });

Copyright © 2019, NetEase, Inc. all right reserved,powered by GitbookFile Modify: 2019-12-18 15:48:24

results matching ""

    No results matching ""