Use Socket IO In Node js to update pug template

Viewed 528

In one of the projects I use Node Js / Express with Pug.js,I want to use socket io to make everything live,all post actions and bring information or update.

I tried to use socket io but all I managed to do was generate html code from js between 2 live users, but i can't figure out how i can use io socket directly in api route / controllers to adapt pug template directly and angular app. To prevent any damage to my app I built a controller and text routes.

  1. server.js

    const app = require('./app');
    
    //Port
    const port = process.env.PORT || '7000';
    
    const server = app.listen(port,()=>{
      console.log(`Server Is Running!!!!`);
    });
    
  2. app.js

     const express = require('express');
      .........
     //App Routes
     const viewRoutes = require('./routes/viewRoutes');
     const postRoutes = require('./routes/postRoutes');
    //Api Routes
    app.use('/api/v2/post',postRoutes);
    app.use('/',viewRoutes);
    
    
    module.exports = app;
    
  1. controllers/postController.js

    /*import mongoose schema*/ 
    const Module = require('../module/testingModule');
    
    //Testing Purpose
    exports.createRandomText = catchAsync(async(req,res)=>{
     const test = await Module.create(req.body);
     console.log(req.body);
    
     res.status(200).json({
        status:'success',
        data:test
       })
    });
    
  2. routes/postRoutes.js

     const postController = require('../controllers/postController');
    
     //Testing Purpose
     router.route('/test-create').post(postController.createRandomText);
    
     module.exports = router;
    
  3. view/testLive.pug

    extends baseac 
    block content
       if(user)
           include header_footer/header_app
           .container
               form.testingPurpopseForm
                  .form-group
                     label(for="exampleInputRandomText") Random Text</label>
                      input(type="text" class="form-control" id="exampleInputRandomText" aria-describedby="emailHelp" placeholder="Random Text")
                 button(type="submit" class="btn btn-primary") Submit
            .container
               ul
                  each ts in test.reverse()
                     li=ts.randomText
    
  4. routes/viewsRoute.js

    //TestingPurpose
     router.route('/test').get(authController.protect,viewsController.testLive);
    
     module.exports = router;
    
  5. views/viewsController.js

    //Testing Purpose
    exports.testLive = catchAsync(async(req,res)=>{
         const getTextTest = await Module.find();
           res.status(200).render('./testLive',{
              test:getTextTest
           });
      });
    

In the oven I made the script by which I take the value from the formula from the pug template and I insert it in the database.

  1. public/js/main.js

    document.querySelector('.testingPurpopseForm').addEventListener('submit',(dataTest)=>{
            dataTest.preventDefault();
    
          const random = document.getElementById('exampleInputRandomText').value;
    
    
          createRandomText(random);
     });
    
  2. public/js/testRandomCreate.js

     import "regenerator-runtime/runtime";
     import axios from 'axios';
    
    
     export const createRandomText  = async(dataRandom) =>{
         try{
             const rand = await axios({
                method:"POST",
                url:"/api/v2/post/test-create",
               data:{
                   randomText:dataRandom
               }
         });
         }catch(err){
            showAlert('error','There was a problem creating yout post!!!');
            console.log(err);
         }    
     }
    

enter image description here

I want when the user posts a random UI text to adapt instantly, I tried to understand the socket documentation I applied some examples from youtube but I create the elements from js and I don't adapt pug template unless I give refresh.

It is possible to use socket io in this example to adapt pug template and also the mobile application

0 Answers
Related