# Rutas

Debemos exponer un endpoint /api/products para nuestro proyecto, es por eso en este punto vamos a realizar dicha tarea.

| Route             | HTTP Verb | Route Middleware   | Description                             |
| ----------------- | --------- | ------------------ | --------------------------------------- |
| /api/products     | GET       |                    | Get list of products                    |
| /api/products     | POST      |                    | Creates a new product                   |
| /api/products/:id | GET       |                    | Get a single user                       |
| /api/products/:id | DELETE    | `hasRole('admin')` | Deletes a product, restriction: 'admin' |

Lo primero será crear nuestro archivo `index.js` dentro `api/product` donde expondremos las rutas asociadas a los verbos HTPP , ejemplo: GET, POST, DELETE

Nuestro archivo `index.js` se verá así:

{% code title="api/product/index.js" %}

```javascript
/**
 * Product
 * @author: Cristian Moreno Zuluaga <khriztianmoreno@gmail.com>
 */

const { Router } = require('express');

const controller = require('./product.controller');

const router = new Router();

router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.create);

module.exports = router;

```

{% endcode %}

Ahora debemos agregar el endpoint `/api/products` al archivo de rutas, el cual es la raíz de todas las rutas y por ende, permite exponer las rutas.

{% code title="routes.js" %}

```javascript
/**
 * Main application routes
 * @author: Cristian Moreno Zuluaga <khriztianmoreno@gmail.com>
 */

// Import Endpoints
const helloWorld = require('./api/helloworld');
// New Line
const product = require('./api/product');

module.exports = (app) => {
  app.use('/api/helloworld', helloWorld);
  // New line
  app.use('/api/products', product);
};
```

{% endcode %}

Podemos luego de esto probar en postman, haciendo una petición POST a [`http://localhost:8080/api/products`](http://localhost:8080/api/products)

![Add the new product](/files/-LhIErWbStjhUqqmvule)

### Repositorio: [Create the products' route](https://github.com/colombia-dev/medjs-workshop-fullstack-js-backend/commit/6536b91eed1c810707cfaacdeb08d75b4d31a858)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://medellin-js.gitbook.io/workshop-fullstack-js-developer/backend/mongoose-js/rutas.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
