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í:

api/product/index.js
/**
 * 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;

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.

routes.js
/**
 * 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);
};

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

Add the new product

Last updated

Was this helpful?