> Portal Navigation: > > - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version. > - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages). > - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`). > - Top-level index of all portals: https://dev.wix.com/docs/llms.txt > - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt # Method name: router(request: WixRouterRequest) # Method package: wixRouter # Method menu location: wixRouter --> router # Method Link: https://dev.wix.com/docs/velo/events-service-plugins/routers/service-plugins/wix-router/router.md # Method Description: Function containing routing logic for a given URL prefix. The `router()` function handles all incoming requests with the specified URL [prefix](#prefixes). 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](https://dev.wix.com/docs/develop-websites/articles/workspace-tools/velo-workspace/workspaces/wix-editor-working-with-the-code-sidebar.md). 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`](wix-router.WixRouterRequest.html) object containing information about the incoming request. The function returns a [`WixRouterResponse`](wix-router.WixRouterResponse.html) 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()`](#ok), [`forbidden()`](#forbidden), [`notFound()`](#notFound), [`redirect()`](#redirect), or [`sendStatus()`](#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()`](https://dev.wix.com/docs/velo/api-reference/wix-window-frontend/get-router-data.md) function of [`wix-window-frontend`](wix-window-frontend.html). 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](#wixrouterrequest/path) contains dashes (`-`). # Method Code Examples: *** Note: do not assume any prop names or enum values other than the ones in the example. ## Router stub ```javascript export function myRouter_Router(request) { // router code ... } ``` ## Basic router ```javascript import {ok, notFound} from "wix-router"; export function myRouter_Router(request) { // URL looks like: // https://mysite.com/myRouter/good // or: // https://user.wixsite.com/mysite/myRouter/good const status = request.path[0]; if(status === "good") { // Show a page return ok("myRouter-page"); } else { // Return 404 return notFound(); } } ``` ## Router that checks requesting devices form factor ```javascript import {ok} from "wix-router"; export function myRouter_Router(request) { if(request.formFactor === "desktop") { // Show desktop page return ok("desktop-page"); } else { // Show mobile page return ok("mobile-page"); } } ``` ## A router with static data ```javascript import {ok, notFound} from "wix-router"; // Sample data const peopleData = { "Ash": { title: "Ash Stowe", imageSite: "https://static.wixstatic.com/media/", image: "b8f383e0fe2b478ea91362b707ef267b.jpg" }, "Aiden": { title: "Aiden Johnson", imageSite: "https://static.wixstatic.com/media/", image: "ca3c7ac5427e43928aa5f3f443ae2163.jpg" }, "Jess": { title: "Jess White", imageSite: "https://static.wixstatic.com/media/", image: "147fe6f37fe24e83977b4124e41b6d3d.jpg" }, "Morgan": { title: "Morgan James", imageSite: "https://static.wixstatic.com/media/", image: "59e1f2f4dbbc4f7c9d6e66e3e125d830.jpg" } }; export function myRouter_Router(request) { // Get item name from URL request // URL looks like: // https://mysite.com/myRouter/Morgan // or: // https://user.wixsite.com/mysite/myRouter/Morgan const name = request.path[0]; // Get the item data by name const data = peopleData[name]; // If the item exists if(data) { //Define SEO tags const seoData = { title: data.title, description: `This is a description of ${data.title} page`, noIndex: false, metaTags: { "og:title": data.title, "og:image": data.imageSite + data.image } }; // Render item page, passing data and head info return ok("myRouter-page", data, seoData); } // Return 404 if the item is not found return notFound(); } ``` ## A router with data from a collection ```javascript import wixData from 'wix-data'; import {ok, notFound} from 'wix-router'; export function myRouter_Router(request) { // Empty path - show index page // URL looks like: // https://mysite.com/myRouter // or: // https://user.wixsite.com/mysite/myRouter if (request.path.length < 1) { return ok("index-page"); } // Path with item - show item page with data from collection // URL looks like: // https://mysite.com/myRouter/itemTitle // or: // https://user.wixsite.com/mysite/myRouter/itemTitle // Retrieve data from collection return wixData.query("myCollection") .eq("title", request.path[0]) .find() .then( (queryResult) => { if (queryResult.length > 0) { return ok("item-page", queryResult.items[0]); } return notFound(); } ); } ``` ## Create a router that reads data from a collection ```javascript // Place this code in the routers.js file // of your site's Backend section. import {ok, notFound} from "wix-router"; import wixData from 'wix-data'; export async function helloworld_Router(request) { console.log('Hello World router is routing...'); if (!request.path[0]) { const { items: greetings } = await wixData.query('Greetings').find(); const seoData = { title: 'Greetings Index', description: 'Index of greetings', noIndex: false }; return ok('index-page', greetings, seoData); } else { const { items: greetings } = await wixData.query('Greetings') .eq('slug', request.path[0]) .find(); if (greetings.length > 0) { const greeting = greetings[0]; const seoData = { title: greeting.title, metaTags: [{ name: 'description', content: greeting.description }, { name: 'og:title', content: greeting.title }], }; return ok('item-page', greeting, seoData); } else { return notFound(); } } } ``` ---