Cherry Documentation

Cherry Documentation

  • Docs
  • Help
  • Blog

›Routing

Getting Started

  • Overview
  • Installation
  • Create a server
  • Adding routes
  • What's next ?

Routing

  • Public resources
  • Basic routes
  • Route contexts
  • Middlewares
  • Redirections

Views & Server response

  • Default error pages
  • Render a html view
  • Render a json payload
  • Download response

Plugins

  • Overview
  • View rendering plugins
  • ORM Plugins
  • Request Plugins
  • Create your own plugins

Miscellaneous features

  • Hooks
  • Server Config
  • Coming Soon...

Best Practices

  • Best Practices

Redirections

Let's talk about the last part the routing chapter.

In a website, we need to redirect our users. There are two kind of redirections :

  • The one in the code which is kind of a technical redirection (like changing a page after the submit of a form)
  • The one before executing the code, for SEO purposes. If you're familiar with Apache, it's like a htaccess

Technical redirections

The first redirection is possible thanks to the method redirect in the response object like this : response.redirect('/my/redirect/url', 301)

The first parameter is the url of the redirection, the second one is optionnal, it's the redirect HTTP code (by default 301).

SEO redirections

Like the routes and the middlewares, it's a new thing to register in the Cherry options. Here is the format of the redirections :

Option nameOptionnalValue TypeDescriptionDefault value
matchUrl❌string|RegExpThe matching pattern to detect if we need to redirect-
targetUrl❌stringThe new route where we want to redirect-
statusCode❌numberThe HTTP code (which is between 300 and 310)-
keepQueryString✔booleanTo know if we keep the query string (the get parameters) or nofalse

So here is an example when you register your redirections :

main.js
config/redirections.js
const Cherry = require('@lund-org/cherry')
// We get our route config
const routes = require('./config/routes.js')
// We get our redirection config
const redirections = require('./config/redirections.js')

const options = {
servers: [
{ port: 80 }
],
routes,
redirections // as simple as the routes and middleware
}
const cherry = new Cherry()
cherry.configure(options)
cherry.start(options)

module.exports = [
{
matchUrl: /^\/old-users\/(.*)/,
targetUrl: '/users/$1',
statusCode: 301
}
]

With only one redirection, all the matching paths beginning with /old-users/ will redirect to /users/xxx. Examples :

  • /old-users/1 will redirect to /users/1
  • /old-users/edit will redirect to /users/edit
  • /old-users/?foo=bar will redirect to /users/ because the keepQueryString has not been defined (so it's false by default)

So it's just a regex to match the routes, and a regex to create the new url

← MiddlewaresDefault error pages →
  • Technical redirections
  • SEO redirections
Cherry Documentation
Docs
Getting StartedGuides (TODO)API Reference (TODO)
Community
User Showcase (TODO)Project ChatTwitter
More
BlogGitHubStar
Copyright © 2020 Lund-Org