Hackernoon logoHow To Configure Routers For Your Website with wix-router by@velo

How To Configure Routers For Your Website with wix-router

Velo is a full-stack development platform that empowers you to rapidly build, manage and deploy professional web apps. Using Velo you can create routers that allow you to take complete control when handling incoming requests to your site. To do so, you set up a router to receive all incoming requests with a specified prefix and define the logic of what to do when a request with that prefix is received. You decide what actions to perform, what response to return, where to route the request, and what data to pass to the page.
image
Velo by Wix Hacker Noon profile picture

@veloVelo by Wix

Velo is a full-stack development platform that empowers you to rapidly build, manage and deploy professional web apps.

Using Velo you can create routers that allow you to take complete control when handling incoming requests to your site. To do so, you set up a router to receive all incoming requests with a specified prefix and define the logic of what to do when a request with that prefix is received. You decide what actions to perform, what response to return, where to route the request, and what data to pass to the page.

You might want to use a router to:

  • Display a dynamic page using content from any data source.
  • Customize your URLs to make them more meaningful and yield better SEO results.
  • Authenticate users and then display content just for them.
  • Return custom HTTP response codes.

The API reference for routers can be found here.

URL Prefix

When creating a router, you choose which requests will get handled by the router based on a URL prefix that you specify. All incoming requests with that URL prefix will be sent to your router for handling. The URL prefix is also used as the router's name.

The prefix is the part of the URL shown in bold in the following examples:

  • Premium site: https://domain.com/prefix/category/item
  • Free site: https://user.wixsite.com/yoursite/prefix/category/item

Routing Code

Your routing logic is defined in the routers.js file, which can be found in the Backend section of your Code Files in the Velo Sidebar. There are two main functions that are the entry points to your router.

They are named with the following convention:

  • <router prefix>_Router(request)
  • <router prefix>_Sitemap(sitemapRequest)

router()

The 

router()
 function is where page requests with the defined prefix are sent. The router receives a WixRouterRequest object containing information about the incoming request. The function then decides what to do with the request and returns the appropriate 
WixRouterResponse
. Typically, the 
router()
 function will decide which page to show (if any) and what data to pass to the page. The response is then sent using the
forbidden()
notFound()
ok()
redirect()
, or
sendStatus()
functions.

sitemap()

The 

sitemap()
 function is where sitemap requests are handled. You can use this function to make sure search engines can find the links to your router's pages. Each 
WixSitemapEntry
 includes information about a page, such as its URL, title, and name.

The 

sitemap()
 function is also used to populate the items preview widget, allowing you to switch between URLs in preview mode.

Router Data

Your 

router()
 function may choose to send data to the pages it routes to. You can access that data in the frontend page code using the
getRouterData()
 function of the wix-window module.

Previously published at https://support.wix.com/en/article/velo-about-routers

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.