DEV Community

Cover image for Adding custom routes in medusa
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

3 2

Adding custom routes in medusa

The cool part about medusa is that we get the option to customize a lot of things.
One of them is routes. This article will show you how to add a store and an admin route.

When it comes to these routes, we add all of them to our server project.
Go ahead and open your server project in your favorite IDE.

Adding a store route

Custom routes are added in the src/app directory. In our case, we'll add an index.js.

import { Router } from 'express';

export default () => {
  const router = Router();

  router.get('/store/ping', (req, res) => {
    res.json({
      message: 'Pong from the server!',
    });
  });

  return router;
};
Enter fullscreen mode Exit fullscreen mode

This will inject a new route under store/ping, which the storefront can use.

Note: the store prefix is according to medusa conventions. You can modify it and return a direct route.

Now let's restart our server and test the endpoint in Insomnia.

Ping request to medusa server

There you go, it works!

Adding an admin route

We can use this same principle to prefix admin routes.
However, the route is now prefixed with admin.

import { Router } from 'express';

export default () => {
  const router = Router();

  router.get('/admin/ping', (req, res) => {
    res.json({
      message: 'Pong from the server!',
    });
  });

  return router;
};
Enter fullscreen mode Exit fullscreen mode

You can go ahead and try this endpoint as well.

Custom admin route in medusa

And that's it. We now learned how to add custom routes to our medusa server.
These routes will come in handy while exploring other options for our admin and front end.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay