【问题标题】:How to put add values on HTML forms in ExpressJS如何在 ExpressJS 中的 HTML 表单上添加值
【发布时间】:2018-11-02 09:54:30
【问题描述】:

我正在尝试将 Javascript 变量值放入 Expression JS 服务器中的 html 表单中,但我不知道如何解决这个问题。

我只想将 x、y 和 res 的值放入 id 为“firstvalue”、“secondvalue”、“result”的表单中,我该怎么做?

我知道这听起来很简单,但我真的迷路了。

// No use of the template system
var express = require('express'),
    logger = require('morgan');
var app = express();
var x = 1;
var y = 2;
var res = x+y;

// Determining the contents of the middleware stack
app.use(logger('dev'));                         // Place an HTTP request recorder on the stack - each request will be logged in the console in 'dev' format
app.use(express.static(__dirname + '/public')); // Place the built-in middleware 'express.static' - static content (files .css, .js, .jpg, etc.) will be provided from the 'public' directory

// Route definitions
app.get('/', function (req, res) { 

    res.writeHead(200, {"Content-Type": "text/html; charset=utf-8"});
    res.write('<p1 id=firstnumber>x</h1>');
    res.write('<p1 id=secondnumber>y</p1>');
    res.write('<p1 id=result>res</h1>');

    res.end();
        // Send a response to the browser
})

// The application is to listen on port number 3000
app.listen(3000, function () {           
    console.log('The application is available on port 3000');
});

【问题讨论】:

    标签: javascript html node.js express


    【解决方案1】:

    您是否尝试过像这样更改您的代码?

    res.write('<p1 id=firstnumber>' + x + '</h1>');
    res.write('<p1 id=secondnumber>' + y + '</p1>');
    res.write('<p1 id=result>' + res + '</h1>');
    

    如果您正在做一些基本的服务器端生成网站,我建议您使用类似express-handlebars-boilerplate

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 2012-11-29
      • 2018-01-31
      • 1970-01-01
      • 2017-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多