Express - SHORT GUIDE
  1. Create github clean project & clone to your pc.
  2. Initialize your backend in root folder of your project by default npm init -y
    Repository, keywords, author, licence, bugs, homepage - we may delete these fields in learning rep.
    Copy from any repo .prettierrc.json
  3. Add script --- "server": "nodemon server.js" --- to package.json for starting backend & create server.js
    (or index.js if you use https://app.cyclic.sh/)
  4. Install main dependencies for backend in root of your project folder by
    npm i express cors dotenv
    and
    npm i nodemon -D
  5. Add simplest express backend server by (also add ".env" file in root of project with necessary PORT=XXXX)

    See the Pen 1-simplest Express web-server by Andrii (@imitator) on CodePen.

    For checking of the server working type in terminal npm run server and look at this on http://localhost:5555/ (or http://127.0.0.1:5555/ )

    Before pushing the initial repository to the github remember about adding .gitignore to the project including in the beginning
    • node_modules/
    • .env
    • package-lock.json
  1. Create -- routes -- folder in our proj for separating routes from main server.js
    • Create file routes/products.routes.js for making routes for products endpoint
    • In routes/products.routes.js create list of products routes by Router() from express
      • const express = require('express');
        const router = express.Router();
    • Create GET response router.get('/', (req, res) => {}), where (req, res) => {} - is a controller.
    To sum up our routes/products.routes.js will be

    See the Pen 3-A __ routes/products.routes.js by Andrii (@imitator) on CodePen.

    • In future we will have more routes (users, customers etc), so we should group them in some kind of routes/index.js where we reexport products
      • const products = require('./products.routes');
        module.exports = { products, };
    • Then import routes in server.js
      • const routes = require('./routes');
    • And use routes in server.js by middleware like
      • app.use('/products', routes.products);
    • sometimes we should change api for new clients, but for old clients we have to live old api, so write in server.js such changes like
      • app.use('/api/v1/products', routes.products);

      For checking our products responding go to http://localhost:5555/api/v1/products

  2. The next step in making convenient web-server is separating controllers from routes folder by creating controllers folder in main project folder.
    • Create controllers/products folder, so move controller from routes/products.routes.js
      • controllers/products/getAllProductsCTRL.js

        const getAllProductsCTRL = (req, res) => {
        res.json([
        {
        name: 'IPhone5',
        price: 10000,
        }
        ]);
        };
        module.exports = getAllProductsCTRL;

    • Reexport functions from controllers folder by making controllers/products/index.js file
      • controller/products/index.js

        const getAllProductsCTRL = require('./getAllProductsCTRL'); module.exports = {getAllProductsCTRL};

    • Import all products controllers from controllers/products folder to controllers/index.js
      • controllers/index.js

        const productsControllers = require('./products'); module.exports = { productsControllers };

    • Import products controllers from controllers folder & use necessary one
      • routes/products.routes.js

        const express = require('express'); const router = express.Router(); const { productsControllers } = require('../controllers'); router.get('/', productsControllers.getAllProductsCTRL); module.exports = router;

      • OR with renaming productsControllers to ctrl when importing controllers

        const express = require('express'); const router = express.Router(); const { productsControllers: ctrl } = require('../controllers'); router.get('/', ctrl.getAllProductsCTRL); module.exports = router;

        P.S. For checking again refresh or go to http://localhost:5555/api/v1/products