router( )


Function containing routing logic for a given URL prefix.

The router() function handles all incoming requests with the specified URL prefix. It is responsible for deciding which page to display and what data to pass to the page, where to redirect to, or which HTTP status code to respond with.

The router() function is not a function that you call from your code. You define the function in a file named routers.js in the Code File's Backend section of the Velo Sidebar. The function is called when your users browse to a URL that is handled by the router with the specified prefix.

The function receives a WixRouterRequest object containing information about the incoming request.

The function returns a WixRouterResponse object that causes the router to respond with a specified page, specified data, and a success response code, or respond with any other HTTP response code.

Typically, the response is created using one of the ok(), forbidden(), notFound(), redirect(), or sendStatus() functions.

Data that is returned as part of the WixRouterResponse is accessed in the page code of the page that was routed to using the getRouterData() function of wix-window-frontend.

You can also set members of the HTML head of the response using the response's HeadOptions parameter.

Notes:

  • The router() function also runs when you navigate to a router page in the Editor.

  • In a request URL, spaces are replaced with dashes (-). For example, a request for /animals/killer whale or /animals/killer%20whale will be received in the request as /animals/killer-whale. Therefore, when searching for the data that matches the incoming request, you need to take special care if the path contains dashes (-).

Method Declaration
Copy
Method Parameters
requestWixRouterRequestRequired

The routing request.

Returns
Return Type:Promise<WixRouterResponse>
Was this helpful?
Yes
No