docs: added dividers + see also section (#2899)
This commit is contained in:
@@ -6,6 +6,8 @@ In this document, you’ll learn how to create endpoints in your Medusa server.
|
||||
|
||||
Custom endpoints reside under the `src/api` directory in your Medusa Backend. They're defined in a TypeScript or JavaScript file that is named `index` (for example, `index.ts`). This file should export a function that returns an Express router.
|
||||
|
||||
---
|
||||
|
||||
## Implementation
|
||||
|
||||
To create a new endpoint, start by creating a new file in `src/api` called `index.ts`. At its basic format, `index.ts` should look something like this:
|
||||
@@ -42,6 +44,8 @@ By Medusa’s conventions:
|
||||
|
||||
You can also create endpoints that don't reside under these two prefixes, similar to the `hello` endpoint in the previous example.
|
||||
|
||||
---
|
||||
|
||||
## CORS Configuration
|
||||
|
||||
If you’re adding a storefront or admin endpoint and you want to access these endpoints from the storefront or Medusa admin, you need to pass your endpoints Cross-Origin Resource Origin (CORS) options using the `cors` package.
|
||||
@@ -94,6 +98,8 @@ router.get("/admin/hello", cors(corsOptions), (req, res) => {
|
||||
})
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Create Multiple Endpoints
|
||||
|
||||
### Same File
|
||||
@@ -185,6 +191,8 @@ export default () => {
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Protected Routes
|
||||
|
||||
Protected routes are routes that should be accessible by logged-in customers or users only.
|
||||
@@ -238,6 +246,8 @@ router.get("/admin/products/count", cors(corsOptions), authenticate(), async (re
|
||||
|
||||
Now, only authenticated users can access this endpoint.
|
||||
|
||||
---
|
||||
|
||||
## Use Services
|
||||
|
||||
Services in Medusa bundle a set of functionalities into one class. Then, you can use that class anywhere in your backend. For example, you can use the `ProductService` to retrieve products or perform operations like creating or updating a product.
|
||||
@@ -260,6 +270,8 @@ router.get("/admin/products/count", cors(corsOptions), authenticate(), (req, res
|
||||
|
||||
The `productService` has a `count` method that returns a Promise. This Promise resolves to the count of the products. You return a JSON of the product count.
|
||||
|
||||
---
|
||||
|
||||
## Building Files
|
||||
|
||||
Custom endpoints must be transpiled and moved to the `dist` directory. This happens when you run your server using `medusa develop` and while it’s running, and when you run the following command:
|
||||
@@ -268,7 +280,10 @@ Custom endpoints must be transpiled and moved to the `dist` directory. This happ
|
||||
npm run build
|
||||
```
|
||||
|
||||
## What’s Next
|
||||
---
|
||||
|
||||
- Check out the available [Admin](https://docs.medusajs.com/api/admin/) and [Storefront](https://docs.medusajs.com/api/store/) APIs.
|
||||
- Learn how to create a [Service](./../services/create-service.md).
|
||||
## See Also
|
||||
|
||||
- [Storefront API Reference](/api/store)
|
||||
- [Admin API Reference](/api/admin)
|
||||
- [Create a Service](./../services/create-service.md).
|
||||
|
||||
Reference in New Issue
Block a user