как я могу добавить цвет / выравнивание / фон к моему ответу. send ("*** какой-то текст ***) или ко всему node.js документу - PullRequest
0 голосов
/ 13 июля 2020

Я новичок в node.js и немного запутался в том, как добавить цвета текста или фон и выравнивание и т.д. c .. в мой node.js документ ... помогите мне с каким-то решением, чтобы я мог получить sum = 12 красным

здесь я предоставил вам некоторый код ... предложите мне несколько способов в его ссылка

                        **app.js**
const express= require("express");
const bodyparser =require("body-parser");
const app=express();
 
 app.use(bodyparser.urlencoded({extended:true}))

  app.get("/",function(req,res){
  res.sendFile( __dirname+"/cal.html");
            
     });
  app.post("/",function(req,res)
  {
    var n1=Number(req.body.num1);
    var n2=Number(req.body.num2);
    var n=n1+n2;

    res.write("<h1>sum is" + n+"</h1>");
   res.send();


   }) 
   app.listen(2000,function()
   {
    console.log("server is running");
       });
                      
          
                           

кал. html (html документ)

<!DOCTYPE html>
<html>
<head>
<title>server </title>
</head>
<body>
    <form action="/" method="post">
    <input type="text" name="num1" value=number1>
    <input type="text" name="num2" placeholder="number3">
    <button type="submit" name="button">calculate</button>
    </form>

 </body>
 </html>

1 Ответ

0 голосов
/ 13 июля 2020

В продолжение моего комментария к вашему вопросу, просто добавьте правильный CSS в свой HTML документ:

Notice res.write('<h1 class="sum">sum is' + n+'</h1>'); в javascript.

Добавлен <h1> в HTML, чтобы показать, что происходит, когда вы добавляете блок <style></style>.

/*
// DISABLED, here for demo only

const express= require("express");
const bodyparser =require("body-parser");
const app=express();
 
 app.use(bodyparser.urlencoded({extended:true}))

  app.get("/",function(req,res){
  res.sendFile( __dirname+"/cal.html");
            
     });
  app.post("/",function(req,res)
  {
    var n1=Number(req.body.num1);
    var n2=Number(req.body.num2);
    var n=n1+n2;

    res.write('<h1 class="sum">sum is ' + n+'</h1>');
   res.send();


   }) 
   app.listen(2000,function()
   {
    console.log("server is running");
       });
*/
<!DOCTYPE html>
<html>
<head>
<title>server </title>

<style>
.sum { color: red }
</style>


</head>
<body>
    <form action="/" method="post">
    <input type="text" name="num1" value=number1>
    <input type="text" name="num2" placeholder="number3">
    <button type="submit" name="button">calculate</button>
    </form>

    <!-- example output -->
    <h1 class="sum">sum is 99999</h1>
 </body>
 </html>
...