Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Make the whole payloadcms backend use /backend as path

default discord avatar
unclaimed10522 years ago
1

Hello guys,



I'm trying to make PayloadCMS use

/backend

as the main subpath, for example, If I want to access the api, instead of

localhost/api

it should be on

localhost/backend/api

. The same for the admin panel, instead of

localhost/admin

it should be on

localhost/backend/admin

.



I tried to edit the payloadcms server.ts with no success, here's what I did


import express from 'express';
import payload from 'payload';

require('dotenv').config();
const app = express();

// Custom middleware function
const backendPrefixMiddleware = (req, res, next) => {
  if (req.path.startsWith('/backend')) {
    req.url = req.url.replace('/backend', '');
  }
  next();
};

// Redirect root to Admin panel
app.get('/', (_, res) => {
  res.redirect('/backend/admin');
});

// Initialize Payload
payload.init({
  secret: process.env.PAYLOAD_SECRET,
  mongoURL: process.env.MONGODB_URI,
  express: app,
  onInit: () => {
    payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`)
  },
})

// Add your own express routes here

// Use the middleware
app.use(backendPrefixMiddleware);

app.listen(3001);

The page

localhost:3001/backend/admin

works with response 200ok, but I only see the background color of the page and nothing else. It seems like the payload is still making requests without appending

/backend

before the path (for example making a request to

http://localhost:3001/api/admins/me

).



Is it possible to achieve what I've described? Thanks!



I solved it, [here is the documentation](

https://payloadcms.com/docs/configuration/overview#options

) that helped me.


Basically I was looking in the wrong place, instead of editing the

server.ts

, I had to go into

payload.config.ts

and add this in the

buildConfig
routes: {
    admin: '/backend/admin',
    api: '/backend/api',
  },

Payload is truly amazing.

    Star on GitHub

    Star

    Chat on Discord

    Discord

    online

    Can't find what you're looking for?

    Get dedicated engineering support directly from the Payload team.