如何在ExpressJS中为HTML表单添加值



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

我只想把x、y和res的值放进id为"firstvalue"、"secondvalue"one_answers"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');
});

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

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

如果你正在做一些基本的服务器端生成的网站,我可以建议你使用类似于这个快速手柄样板的东西吗

最新更新