Diego Ulloa
Diego Ulloa

Reputation: 639

Strapi: Add prefix to api endpoints

I have a Strapi instance running at localhost, where I need to prefix the api convention /api/v1 to the URL, but only for api endpoints. I can't find the way.

I already have this in server.js

server.js

  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  url: env('', 'http://localhost:1337'),
  admin: {
    auth: {
      secret: env('ADMIN_JWT_SECRET', '9c27e32146600c92d6fccb208d1fc873'),
    },
  },
});

So I need to request the data at endpoints like: http://localhost:1337/api/v1/restaurant/:id

And access to admin like default: http://localhost:1337/admin

Is that possible? I'm using [email protected]

Upvotes: 1

Views: 3403

Answers (3)

rajratna maitry
rajratna maitry

Reputation: 508

nginx config

http {
upstream strapi {
    server 127.0.0.1:1337;
}
    server {
    listen       8888;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;
    # location / {
    #     proxy_connect_timeout 180s;
    #     proxy_read_timeout 5400s;
    #     proxy_send_timeout 5400s;
    #     proxy_pass    http://127.0.0.1:1337/;
    # }
    location / {
        root C:/projects/strapi;
    }
    location /service-catalog/ {
        rewrite ^/service-catalog/(.*) /$1  break;
        proxy_pass http://strapi;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass_request_headers on;
    }
    location /service-catalog/assets/ {
        rewrite /service-catalog/(.*) /$1  break;
        proxy_pass http://strapi/assets;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass_request_headers on;
    }
    location /dashboard {
        proxy_pass http://strapi/dashboard;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass_request_headers on;
    }

} }

server.js

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  url: 'http://localhost:8888/service-catalog',
  admin: {
    url:'http://localhost:8888/dashboard',
    auth: {
      secret: env('ADMIN_JWT_SECRET', 'f8bbc8130154293e157e1e01ab09d62d'),
    },
  },
});

delete build .cache folder then run npm run build then run npm run develop

Upvotes: 4

Jeet
Jeet

Reputation: 13

You can change your endpoint from api config. route.json

For your case you can try this. go to ../app/api/restaurant/config/route.json

{
  "method": "GET",
  "path": "/api/v1/restaurants/:id",  # update your route here
  "handler": "restaurant.findOne",    #action handler
  "config": {
    "policies": []
  }
},...

now you can try http://localhost:1337/api/v1/restaurant/:id

You have to do the same for each route.

For more - https://strapi.io/documentation/v3.x/content-api/api-endpoints.html#endpoints

Upvotes: 1

Ian Hoar
Ian Hoar

Reputation: 1184

I don't believe this is possible without using a proxy.

In your server.js you would specify url: 'https://api.example.com/v1', but this would also need to be set up in your proxy as this only makes Strapi aware of the proxy.

More details here:

https://strapi.io/documentation/v3.x/deployment/nginx-proxy.html#nginx-proxying

Upvotes: 2

Related Questions