Where to add a new route in Parse Server

Hi, I need to add a new route for my app users to generate tokens. I checked parse server code base. It seems that I can add a custom route in ./src/ParseServer.js. Is this the best place to add a custom route? Or, should I create an independent custom route outside Parse Server routes? Like directly in express.js? Thanks

Maybe you can use cloud function?

@uzaysan What is cloud function? I looked at the document. There are not much information there.

Instead of forking and modifying the Parse Server codebase you can just add a route on the express level as you suggested. That would be the usual approach.

Depending on the type of route you want to add, this may also be interesting for you:

@uzaysan This is very helpful. But what is the real advantage to use a cloud function instead of adding a custom route in ./src/ParseServer.js? The only thing I can think of is “When you use a cloud function, the entire Parse JavaScript SDK is available in the cloud environment. So you can use that to query over Parse objects”. However, if I add a custom route in ./src/ParseServer.js, I can also access the Parse objects somehow. Just possibly more difficult to do it.

I would prefer cloud code ıf cloud code meets the requirements. No need to re invent wheel. Of course if you want to, you can define seperate express routes or even modify the source.

Its easier.

@uzaysan I agree with you. Thanks

@Manuel In the link you posted, it says " This feature is only implemented in the still experimental PagesRouter". Is this feature still experimental?

@hkblue It should be safe to use, because it was introduced more than half a year ago and is currently used extensively in production environments. We will move the feature out of experimental status for the upcoming release of Parse Server 5. See Deprecate current pages implementation · Issue #7625 · parse-community/parse-server · GitHub.

@Manuel Thanks. But why is the approach so unique? You still changed Parse server codebase, right?

pages.customRoutes: [
    {
      method: 'GET',
      path: 'custom_route',
      handler: async req => {
        // some custom logic
        // ...
        // then, depending on the outcome, return a HTML file as response
        return { file: 'custom_page.html' };
      },
    },
]

Before we had this approach, what did we do to add a custom url to express.js?
Also, you mentioned

" The custom routes are mounted after the built-in routes like password reset. That means a custom route can not accidentally “steal” a request from a built-in route, because requests always are first passed to the built-in routes for a possible match."

How is this feature achieved? Thanks

The approach is useful because it gives a standardized way to build custom routes and automatically passes parameters like appId and serverUrl for you to use.

The custom routes are mounted after default routes like password reset and email verification. That means if you accidentally define a custom route with the same name as a built-in route, the request would not reach your custom route.