Re-direct to a URL from the backend

As mentioned in the article we can use wix-location API only from the frontend
https://www.wix.com/velo/reference/wix-location/introduction#wix-location_introduction_introduction

But I want to redirect to a URL from the backend. Is there any way to do so - maybe using some npm package or basic javascript?


I know that we can return a response from the backend to the frontend and then use wix-location API but I want to learn a new way to do so since that could save one extra step :grinning:

I don’t think that is possible (I maybe mistaken).

The issue is that what runs in the browser and what runs in the backend (server) are 2 different locations.

When you redirect to a new page, it is your browser that performs that task. You browser is responsible for displaying pages and going to new websites and loading their pages.

The stuff that runs in the backend, runs on a server and not on your desktop/mobile device etc. Since the backend is not running in a brower, it would not make sense for a redirect to work in the backend.

The backend server, just runs code in a javascript (or similar type) file. It has no sense of any other web address location. It can use folders that are local to it to fetch/save information to. The code running in the backend can also reach out to another external server on the web through an external API call. It can interact with another external server to get data/send data etc.

Hi there,

You can redirect requests to access router pages on the backend when a router URL is requested.

On the backend, inside your routers.js file, and assuming that you’ve created a router with a prefix of " redirect ".

import { redirect, notFound } from 'wix-router';

export function redirect_Router(request) {
    const { path } = request;
    if (path[0] === 'nasriya') {
        return redirect('https://nasriya.net', 307);
    }
    
    return notFound();
}

export function redirect_SiteMap(request) {
    return [];
}

In the code above, any requests coming to “/redirect/nasriya” will redirect the visitor to “https://nasriya.net”.

Please note that this only works on the URL when it’s visited, it can’t be ran if otherwise.

Hope this helps~!
Ahmad