wix-router.WixRouterResponse

wix-router.WixRouterResponse

An object representing a response to a request received by a router.

Contents

data Sets or gets the data to pass with the response.
head Sets or gets the members to be written to the HTML head of the page.
message Sets or gets the response message to be used when responding with status codes 4xx and 5xx.
page Sets or gets the page to route to.
redirectUrl Sets or gets the url to redirect to when responding with status codes 301 and 302.
status Sets or gets the response's HTTP status code.
HeadOptions Members of the HTML head.
data

data

Sets or gets the data to pass with the response.

Syntax

get data(): Object
set data(value: Object): void

Description

A data object to be used by the page routed to. The page routed to accesses the data passed to it using the getRouterData() function of wix-window. with optional data and HTML header.

Type

Object

Examples

Get the data that is passed with the response

ZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIGxldCBkYXRhID0gcmVzcG9uc2UuZGF0YTsKICAvKgogICogewogICogICAidGl0bGUiOiAiQSIsCiAgKiAgICJpbWFnZVNpdGUiOiAiaHR0cDovL3N0YXRpYy53aXhzdGF0aWMuY29tL21lZGlhLyIsCiAgKiAgICJpbWFnZSI6ICJiOGYzODNlMGZlMmI0NzhlYTkxMzYyYjcwN2VmMjY3Yi5qcGciCiAgKiB9CiAgKi8KCn0K
export function myRouter_Router(request) {

  let data = response.data;
  /*
  * {
  *   "title": "A",
  *   "imageSite": "http://static.wixstatic.com/media/",
  *   "image": "b8f383e0fe2b478ea91362b707ef267b.jpg"
  * }
  */

}

Set the data that is passed with the response

ZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIHJlc3BvbnNlLmRhdGEgPSB7CiAgICAidGl0bGUiOiAiQSIsCiAgICAiaW1hZ2VTaXRlIjogImh0dHA6Ly9zdGF0aWMud2l4c3RhdGljLmNvbS9tZWRpYS8iLAogICAgImltYWdlIjogImI4ZjM4M2UwZmUyYjQ3OGVhOTEzNjJiNzA3ZWYyNjdiLmpwZyIKICB9OwoKfQo=
export function myRouter_Router(request) {

  response.data = {
    "title": "A",
    "imageSite": "http://static.wixstatic.com/media/",
    "image": "b8f383e0fe2b478ea91362b707ef267b.jpg"
  };

}
head

head

Sets or gets the members to be written to the HTML head of the page.

Syntax

get head(): HeadOptions
set head(value: HeadOptions): void

Type

HeadOptions

Examples

Get the response's head options

ZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIGxldCBoZWFkID0gcmVzcG9uc2UuaGVhZDsKCiAgLyoKICAqIHsKICAqICAgInRpdGxlIjogIkEgcGFnZSB0aXRsZSIsCiAgKiAgICJkZXNjcmlwdGlvbiI6ICJBIHBhZ2UgZGVzY3JpcHRpb24iLAogICogICAia2V5d29yZHMiOiAid2l4IGNvZGUgZXhhbXBsZSIsCiAgKiAgICJub0luZGV4IjogdHJ1ZSwKICAqICAgIm1ldGFUYWdzIjogewogICogICAgICJjb3B5cmlnaHQiOiAiV2l4LmNvbSIsCiAgKiAgICAgIkNsYXNzaWZpY2F0aW9uIjogIkV4YW1wbGUiLAogICogICAgICJvZzp0aXRsZSI6ICJUaGUgUm9jayIsCiAgKiAgICAgIm9nOmltYWdlIjogImh0dHA6Ly9pYS5tZWRpYS1pbWRiLmNvbS9yb2NrLmpwZyIKICAqICAgfQogICogfQogICovCgp9Cg==
export function myRouter_Router(request) {

  let head = response.head;

  /*
  * {
  *   "title": "A page title",
  *   "description": "A page description",
  *   "keywords": "wix code example",
  *   "noIndex": true,
  *   "metaTags": {
  *     "copyright": "Wix.com",
  *     "Classification": "Example",
  *     "og:title": "The Rock",
  *     "og:image": "http://ia.media-imdb.com/rock.jpg"
  *   }
  * }
  */

}

Set the response's head options

ZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIGxldCBoZWFkT3B0aW9ucyA9IHsKICAgICJ0aXRsZSI6ICJBIHBhZ2UgdGl0bGUiLAogICAgImRlc2NyaXB0aW9uIjogIkEgcGFnZSBkZXNjcmlwdGlvbiIsCiAgICAia2V5d29yZHMiOiAid2l4IGNvZGUgZXhhbXBsZSIsCiAgICAibm9JbmRleCI6IHRydWUsCiAgICAibWV0YVRhZ3MiOiB7CiAgICAgICJjb3B5cmlnaHQiOiAiV2l4LmNvbSIsCiAgICAgICJDbGFzc2lmaWNhdGlvbiI6ICJFeGFtcGxlIiwKICAgICAgIm9nOnRpdGxlIjogIlRoZSBSb2NrIiwKICAgICAgIm9nOmltYWdlIjogImh0dHA6Ly9pYS5tZWRpYS1pbWRiLmNvbS9yb2NrLmpwZyIKICAgIH0KICB9OwoKICBsZXQgcmVzcG9uc2UuaGVhZCA9IGhlYWRPcHRpb25zOwoKfQo=
export function myRouter_Router(request) {

  let headOptions = {
    "title": "A page title",
    "description": "A page description",
    "keywords": "wix code example",
    "noIndex": true,
    "metaTags": {
      "copyright": "Wix.com",
      "Classification": "Example",
      "og:title": "The Rock",
      "og:image": "http://ia.media-imdb.com/rock.jpg"
    }
  };

  let response.head = headOptions;

}
message

message

Sets or gets the response message to be used when responding with status codes 4xx and 5xx.

Syntax

get message(): string
set message(value: string): void

Type

String

Examples

Get the response message

ZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIGxldCBtZXNzYWdlID0gcmVzcG9uc2UubWVzc2FnZTsgLy8gIk5vdCBmb3VuZCIKCn0K
export function myRouter_Router(request) {

  let message = response.message; // "Not found"

}

Set the response message

ZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIHJlc3BvbnNlLm1lc3NhZ2UgPSAiTm90IGZvdW5kIjsKCn0K
export function myRouter_Router(request) {

  response.message = "Not found";

}
page

page

Sets or gets the page to route to.

Syntax

get page(): string
set page(value: string): void

Type

String

Examples

Get the page the response routes to

ZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIGxldCBwYWdlID0gcmVzcG9uc2UucGFnZTsgLy8gIm15Um91dGVyLXBhZ2UiCgp9Cg==
export function myRouter_Router(request) {

  let page = response.page; // "myRouter-page"

}

Set the page the response routes to

ZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIHJlc3BvbnNlLnBhZ2UgPSAibXlSb3V0ZXItcGFnZSI7Cgp9Cg==
export function myRouter_Router(request) {

  response.page = "myRouter-page";

}
redirectUrl

redirectUrl

Sets or gets the url to redirect to when responding with status codes 301 and 302.

Syntax

get redirectUrl(): string
set redirectUrl(value: string): void

Type

String

Examples

Get the URL to redirect to

ZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIGxldCByZWRpcmVjdFVybCA9IHJlc3BvbnNlLnJlZGlyZWN0VXJsOwogIC8vICJodHRwOi8vc29tZW90aGVyc2l0ZS5jb20iCgp9Cg==
export function myRouter_Router(request) {

  let redirectUrl = response.redirectUrl;
  // "http://someothersite.com"

}

Set the URL to redirect to

ZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIHJlc3BvbnNlLnJlZGlyZWN0VXJsID0gImh0dHA6Ly9zb21lb3RoZXJzaXRlLmNvbSI7Cgp9Cg==
export function myRouter_Router(request) {

  response.redirectUrl = "http://someothersite.com";

}
status

status

Sets or gets the response's HTTP status code.

Syntax

get status(): Number
set status(value: Number): void

Type

Number

Examples

Get the response's HTTP status code

ZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIGxldCBzdGF0dXMgPSByZXNwb25zZS5zdGF0dXM7IC8vIDIwMAoKfQo=
export function myRouter_Router(request) {

  let status = response.status; // 200

}

Set the response's HTTP status code

ZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIHJlc3BvbnNlLnN0YXR1cyA9IDIwMDsKCn0K
export function myRouter_Router(request) {

  response.status = 200;

}
HeadOptions

HeadOptions

Members of the HTML head.

Description

The HeadOptions object defines the HTML head of the page to respond with. The members of the HeadOptions are written to the HTML head for use by browsers and SEO bots.

Properties

title String The page title. Written to the <title> tag.
description String The page description. Written to the <meta name="description" content="<value>"> tag.
keywords String The page keywords. Written to the <meta name="keywords" content="<value>"> tag.
noIndex Boolean Indicates whether to add a meta tag that prevents search engines from indexing the page. Setting noIndex to true will add <meta name="robots" content="noindex">.
metaTags Object Other meta tags of the head. An object with key:value pairs where the key is the meta tag name and the value is the content.

Examples

HeadOptions in page head

Ly8gR2l2ZW4gdGhlIGZvbGxvd2luZyBIZWFkT3B0aW9ucwp7CiAgInRpdGxlIjogIkEgcGFnZSB0aXRsZSIsCiAgImRlc2NyaXB0aW9uIjogIkEgcGFnZSBkZXNjcmlwdGlvbiIsCiAgImtleXdvcmRzIjogIndpeCBjb2RlIGV4YW1wbGUiLAogICJub0luZGV4IjogdHJ1ZSwKICAibWV0YVRhZ3MiOiB7CiAgICAiY29weXJpZ2h0IjogIldpeC5jb20iLAogICAgIkNsYXNzaWZpY2F0aW9uIjogIkV4YW1wbGUiLAogICAgIm9nOnRpdGxlIjogIlRoZSBSb2NrIiwKICAgICJvZzppbWFnZSI6ICJodHRwOi8vaWEubWVkaWEtaW1kYi5jb20vcm9jay5qcGciCiAgfQp9CgovLyBUaGUgSFRNTCBwYWdlIGhlYWQgd2lsbCBjb250YWluCiZsdDtoZWFkJmd0OwogICZsdDt0aXRsZSZndDtBIHBhZ2UgdGl0bGUmbHQ7L3RpdGxlJmd0OwogICZsdDttZXRhIG5hbWU9ImRlc2NyaXB0aW9uIiBjb250ZW50PSJBIHBhZ2UgZGVzY3JpcHRpb24iLyZndDsKICAmbHQ7bWV0YSBuYW1lPSJrZXl3b3JkcyIgY29udGVudD0id2l4IGNvZGUgZXhhbXBsZSIvJmd0OwogICZsdDttZXRhIG5hbWU9InJvYm90cyIgY29udGVudD0ibm9pbmRleCImZ3Q7CiAgJmx0O21ldGEgbmFtZT0iY29weXJpZ2h0IiBjb250ZW50PSJXaXguY29tIi8mZ3Q7CiAgJmx0O21ldGEgbmFtZT0iQ2xhc3NpZmljYXRpb24iIGNvbnRlbnQ9IkV4YW1wbGUiLyZndDsKICAmbHQ7bWV0YSBuYW1lPSJvZzp0aXRsZSIgY29udGVudD0iVGhlIFRpdGxlIi8mZ3Q7CiAgJmx0O21ldGEgbmFtZT0ib2c6aW1hZ2UiIGNvbnRlbnQ9Imh0dHA6Ly9zb21lc2l0ZS9zb21laW1hZ2UuanBnIi8mZ3Q7CiZsdDsvaGVhZCZndDsK
// Given the following HeadOptions
{
  "title": "A page title",
  "description": "A page description",
  "keywords": "wix code example",
  "noIndex": true,
  "metaTags": {
    "copyright": "Wix.com",
    "Classification": "Example",
    "og:title": "The Rock",
    "og:image": "http://ia.media-imdb.com/rock.jpg"
  }
}

// The HTML page head will contain
<head>
  <title>A page title</title>
  <meta name="description" content="A page description"/>
  <meta name="keywords" content="wix code example"/>
  <meta name="robots" content="noindex">
  <meta name="copyright" content="Wix.com"/>
  <meta name="Classification" content="Example"/>
  <meta name="og:title" content="The Title"/>
  <meta name="og:image" content="http://somesite/someimage.jpg"/>
</head>

Get the head options of the response

ZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIGxldCBoZWFkID0gcmVzcG9uc2UuaGVhZDsKCiAgLyoKICAqIHsKICAqICAgInRpdGxlIjogIkEgcGFnZSB0aXRsZSIsCiAgKiAgICJkZXNjcmlwdGlvbiI6ICJBIHBhZ2UgZGVzY3JpcHRpb24iLAogICogICAia2V5d29yZHMiOiAid2l4IGNvZGUgZXhhbXBsZSIsCiAgKiAgICJub0luZGV4IjogdHJ1ZSwKICAqICAgIm1ldGFUYWdzIjogewogICogICAgICJjb3B5cmlnaHQiOiAiV2l4LmNvbSIsCiAgKiAgICAgIkNsYXNzaWZpY2F0aW9uIjogIkV4YW1wbGUiLAogICogICAgICJvZzp0aXRsZSI6ICJUaGUgUm9jayIsCiAgKiAgICAgIm9nOmltYWdlIjogImh0dHA6Ly9pYS5tZWRpYS1pbWRiLmNvbS9yb2NrLmpwZyIKICAqICAgfQogICogfQogICovCgp9Cg==
export function myRouter_Router(request) {

  let head = response.head;

  /*
  * {
  *   "title": "A page title",
  *   "description": "A page description",
  *   "keywords": "wix code example",
  *   "noIndex": true,
  *   "metaTags": {
  *     "copyright": "Wix.com",
  *     "Classification": "Example",
  *     "og:title": "The Rock",
  *     "og:image": "http://ia.media-imdb.com/rock.jpg"
  *   }
  * }
  */

}

Set the page options of the response

ZXhwb3J0IGZ1bmN0aW9uIG15Um91dGVyX1JvdXRlcihyZXF1ZXN0KSB7CgogIGxldCBoZWFkT3B0aW9ucyA9IHsKICAgICJ0aXRsZSI6ICJBIHBhZ2UgdGl0bGUiLAogICAgImRlc2NyaXB0aW9uIjogIkEgcGFnZSBkZXNjcmlwdGlvbiIsCiAgICAia2V5d29yZHMiOiAid2l4IGNvZGUgZXhhbXBsZSIsCiAgICAibm9JbmRleCI6IHRydWUsCiAgICAibWV0YVRhZ3MiOiB7CiAgICAgICJjb3B5cmlnaHQiOiAiV2l4LmNvbSIsCiAgICAgICJDbGFzc2lmaWNhdGlvbiI6ICJFeGFtcGxlIiwKICAgICAgIm9nOnRpdGxlIjogIlRoZSBSb2NrIiwKICAgICAgIm9nOmltYWdlIjogImh0dHA6Ly9pYS5tZWRpYS1pbWRiLmNvbS9yb2NrLmpwZyIKICAgIH0KICB9OwoKICBsZXQgcmVzcG9uc2UuaGVhZCA9IGhlYWRPcHRpb25zOwoKfQo=
export function myRouter_Router(request) {

  let headOptions = {
    "title": "A page title",
    "description": "A page description",
    "keywords": "wix code example",
    "noIndex": true,
    "metaTags": {
      "copyright": "Wix.com",
      "Classification": "Example",
      "og:title": "The Rock",
      "og:image": "http://ia.media-imdb.com/rock.jpg"
    }
  };

  let response.head = headOptions;

}

Create an okay response with data and head options

aW1wb3J0IHtva30gZnJvbSAnd2l4LXJvdXRlcic7CgpleHBvcnQgZnVuY3Rpb24gbXlSb3V0ZXJfUm91dGVyKHJlcXVlc3QpIHsKCiAgbGV0IGRhdGFPYmogPSB7CiAgICAiZmllbGQxIjogInZhbHVlMSIsCiAgICAiZmllbGQyIjogInZhbHVlMiIKICB9OwoKICBsZXQgaGVhZE9wdGlvbnMgPSB7CiAgICAidGl0bGUiOiAiQSBwYWdlIHRpdGxlIiwKICAgICJkZXNjcmlwdGlvbiI6ICJBIHBhZ2UgZGVzY3JpcHRpb24iLAogICAgImtleXdvcmRzIjogIndpeCBjb2RlIGV4YW1wbGUiLAogICAgIm5vSW5kZXgiOiB0cnVlLAogICAgIm1ldGFUYWdzIjogewogICAgICAiY29weXJpZ2h0IjogIldpeC5jb20iLAogICAgICAiQ2xhc3NpZmljYXRpb24iOiAiRXhhbXBsZSIsCiAgICAgICJvZzp0aXRsZSI6ICJUaGUgUm9jayIsCiAgICAgICJvZzppbWFnZSI6ICJodHRwOi8vaWEubWVkaWEtaW1kYi5jb20vcm9jay5qcGciCiAgICB9CiAgfTsKCiAgcmV0dXJuIG9rKCJyb3V0ZXItcGFnZSIsIGRhdGFPYmosIGhlYWRPcHRpb25zKTsKfQo=
import {ok} from 'wix-router';

export function myRouter_Router(request) {

  let dataObj = {
    "field1": "value1",
    "field2": "value2"
  };

  let headOptions = {
    "title": "A page title",
    "description": "A page description",
    "keywords": "wix code example",
    "noIndex": true,
    "metaTags": {
      "copyright": "Wix.com",
      "Classification": "Example",
      "og:title": "The Rock",
      "og:image": "http://ia.media-imdb.com/rock.jpg"
    }
  };

  return ok("router-page", dataObj, headOptions);
}