Javascript HTML客户端和Node.js服务器之间的变量
我只想将变量从HTML页面传递到节点js,并对数据执行一些计算,然后使用ejs将其返回HTML 安装ejs后:Javascript HTML客户端和Node.js服务器之间的变量,javascript,node.js,ejs,Javascript,Node.js,Ejs,我只想将变量从HTML页面传递到节点js,并对数据执行一些计算,然后使用ejs将其返回HTML 安装ejs后: npm install ejs 我正在尝试传递值为50的变量temp“HTML页面”: 任何帮助都将不胜感激 提前感谢。在服务器文件中,temp的值未定义。所以total=未定义+10=未定义。因此,服务器文件中未定义这两个变量。 尝试执行此操作(在服务器文件中): var-temp=0 var总计=0 在html文件My temperature中: 温度+10: 现在它应该显示正确
npm install ejs
我正在尝试传递值为50的变量temp“HTML页面”:
任何帮助都将不胜感激
提前感谢。在服务器文件中,temp的值未定义。所以total=未定义+10=未定义。因此,服务器文件中未定义这两个变量。 尝试执行此操作(在服务器文件中):
var-temp=0
var总计=0
在html文件My temperature中:
温度+10:
现在它应该显示正确的值,即50和60。希望这对您有所帮助您不需要此
fs
要求
您需要做的只是:
var express = require('express');
var app = express();
var path = require('path'); //Use the path to tell where find the .ejs files
// view engine setup
app.set('views', path.join(__dirname, 'views')); // here the .ejs files is in views folders
app.set('view engine', 'ejs'); //tell the template engine
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) { // route for '/'
var temp = 50; //here you assign temp variable with needed value
var total = temp+10;
res.render('index', { //render the index.ejs
temp: temp,
total:total
});
});
var server = app.listen(3000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
HTML(重命名为index.ejs):
我的体温:
温度+10:
通过这种方式,我不能在我的“server.js”中使用值为“50”的temp,我需要使用它的值,我将通过HTML页面传递它。因此,您将在HTML页面上传递一个值,并希望在服务器文件上使用它。。正确的?但是在您编写的代码中,您没有在html页面上传递任何动态值。您所做的是硬编码temp的值。您应该向页面添加一个输入字段,然后动态存储值,供服务器文件使用。你可以使用angular。我想从HTML页面BTW@Hat传递临时值,但这不是最佳做法,如果你想使用HTML中的值,我建议你在HTML内容中创建一个javascript/文本,然后通过javascript(而不是服务器端)更新/使用valuesOk@Alvardo,这是我更新的代码设置温度:
01123456c
函数myFunction(){var h=document.getElementById(“temp”);var option_1=h.options[h.selectedIndex].text;temp=Number(option_1);}
是否可以将temp变量传递到server.js文件?
var http = require('http');
var ejs = require('ejs');
var fs = require('fs');
http.createServer(function(req,res) {
res.writeHead(200, {'Content-Type': 'text/html'});
//since we are in a request handler function
//we're using readFile instead of readFileSync
fs.readFile('index.html', 'utf-8', function(err, content) {
if (err) {
res.end('error occurred');
return;
}
var temp; //here you assign temp variable with needed value
var total = temp+10;
var renderedHtml = ejs.render(content, {temp: temp, total:total}); //get redered HTML code
res.end(renderedHtml);
});
}).listen(8080);
var express = require('express');
var app = express();
var path = require('path'); //Use the path to tell where find the .ejs files
// view engine setup
app.set('views', path.join(__dirname, 'views')); // here the .ejs files is in views folders
app.set('view engine', 'ejs'); //tell the template engine
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) { // route for '/'
var temp = 50; //here you assign temp variable with needed value
var total = temp+10;
res.render('index', { //render the index.ejs
temp: temp,
total:total
});
});
var server = app.listen(3000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
<html>
<head>
</head>
<body>
My temperature: <%= temp %>
Temp + 10 : <%= total %>
</body>
</html>