Create a Simple API:
Adding More Routes (Video 3)

This tutorial is based exclusively on Maximillian Schwarzmüller's Creating a REST API with Node.js starting with Part 3 Adding More Routes. It shows just the coding occurring in video 3.

  1. Create a new folder in the directory you have been using named api.
  2. In that folder, create a subfolder named routes.
  3. In routes, create products.js. In this file, enter:
    const express = require('express');
    const router = express.Router();
    router.get('/', (req, res, next) => {
        res.status(200).json({
            message: 'Handling GET requests to /products'
        });
    });
    router.post('/', (req, res, next) => {
        res.status(201).json({
            message: 'Handling POST requests to /products'
        });
    });
    module.exports = router;
  4. Revise app.js to look like this:
    const express = require('express'); // this refers to express just installed
    const app = express();
    const productRoutes = require('./api/routes/products');
    app.use('/products', productRoutes);
    module.exports = app;
  5. Return to Postman and run GET on localhost:3000. It returns an error, because the code includes no way to handle that sort of request.
  6. Run POST on localhost:3000/products and see this response:
    "message": "Handling POST requests to /products"
  7. Run GET on localhost:3000/products and see this response:
    "message": "Handling GET requests to /products"
  8. Now to enable requests for specific product IDs, revise products.js to look this:
    const express = require('express');
    const router = express.Router();
    router.get('/', (req, res, next) => {
        res.status(200).json({
            message: 'Handling GET requests to /products'
        });
    });
    router.post('/', (req, res, next) => {
        res.status(201).json({
            message: 'Handling POST requests to /products'
        });
    });
    router.get('/:productId', (req, res, next) => { // gets reponse on specific iD
         const id = req.params.productId;
         if (id === 'special') {
            res.status(200).json({
                 message: 'You found the spcial D',
                 id: id
             });
        } else {
             res.status(200).json({
                message: 'You passed an ID'
            });
        }
    });
  9. Because we have changed something on the server, we need to restart the server: node server.js
  10. Return to Postman and run GET on localhost:3000/products/123 and press Send: the response is:
    "message": "You passed an ID"
  11. Run GET on localhost:3000/products/specialspan>; the response is:
    "message": "You found the spcial D",
    "id": "special"
  12. To support PATCH and DELETE routes, add these lines to products.js just above the module.exports = router; line:
    router.patch('/:productId', (req, res, next) => {
         res.status(200).json({
             message: 'Updated product'
         });
    });
    router.delete('/:productId', (req, res, next) => {
        res.status(200).json({
             message: 'Deleted product'
       });
    });  
  13. Run PATCH on localhost:3000/products/special and get this: "message": "Updated product"
  14. Run DELETE on localhost:3000/products/special and get this: "message": "Deleted product"
  15. But if you run PUT on localhost:3000/products/special, you get an an error because there is not route for that requests.
  16. To begin the process of handling orders, add orders.js to the routes subdirectory.
  17. Add these lines to orders.js:
    const express = require('express');
    const router = express.Router();
    router.get('/', (req, res, next) => {
        res.status(200).json({
            message: 'Orders were fetched'
        });
    });
    router.post('/', (req, res, next) => {
        res.status(201).json({
            message: 'Order was created'
        });
    });
    router.get('/:orderId', (req, res, next) => {
        res.status(200).json({
            message: 'Order details',
            orderId: req.params.orderId
        });
    });
    router.delete('/:orderId', (req, res, next) => {
        res.status(200).json({
            message: 'Order deleted',
            orderId: req.params.orderId
        });
    });
    module_exports = router;
  18. Go back to products.js and change 200 to 201 in the router.post segment.
  19. To enable orders requests to function go to app.js and revise thec file to look this way:
    const express = require('express'); // this refers to express just installed
    const app = express();
    const productRoutes = require('./api/routes/products');
    const orderRoutes = require('./api/routes/orders');
    app.use('/products', productRoutes);
    app.use('/orders', orderRoutes);
    module.exports = app;
  20. Restart the server, go to Postman.
  21. Run GET localhost:3000/products/order NO WORKIE! "message": "Updated product"
  22. Run POST on localhost:3000/orders NO WORKIE!

Information and programs provided by admin@mcmassociates.io.