0
app.post('/result.html',function(req,res){
  var num1 = req.body.Num1 ;
  var num2 = req.body.Num2 ;
  var operator = req.body.Operator ;
  var result =0 ;
  switch (operator) {
    case '+':
      result = Number(num1)+Number(num2) ;
      break;
    case '-':
      result = Number(num1)-Number(num2) ;
      break;
    case '*':
      result = Number(num1)*Number(num2) ;
      break;
    default:
      result="Invalid Operator.";
  }

/**/This line is giving me error..................HOW TO USE document ?????
//*
  document.querySelector(".result").innerHTML = "Result is : "+result ;

  res.sendFile(__dirname+'/result.html');
});

I have also included scipt tag in my result.html, but I am unable to use document with express, please help me , I am new to node?

  • Does this answer your question? [Can I do DOM manipulation within an Express POST request?](https://stackoverflow.com/questions/52356063/can-i-do-dom-manipulation-within-an-express-post-request) – kuzditomi Aug 13 '20 at 06:19
  • take a look: https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming learn the very basics – bill.gates Aug 13 '20 at 06:22
  • the typical way is alter your DOM on the `frontend`, however if you want to do it on the backend side. you need to virtually render it first – Karl L Aug 13 '20 at 06:23

1 Answers1

1

Node.js is a runtime for javascript, it doesn't have access to DOM, so with the express or any other library dedicated for the server-side, you cannot access DOM.

If you want to access DOM, you will need a frontend. Search for view engine in node js (like ejs, pug, jade, etc) these are dedicated for frontend and will have access to dom (from script file)

If possible keep frontend (React/Angular/Vue etc) separate from the backend (NodeJs).

r7r
  • 1,440
  • 1
  • 11
  • 19