如何在nodejs中设置html中的文本字段值? [英] how to set value of text field in html from nodejs?

查看:948
本文介绍了如何在nodejs中设置html中的文本字段值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在nodejs和html中做项目。可以帮助人们如何在server.js的html中设置值到文本字段。例如,我在index.html上使用id为'name'的文本字段。我使用res.body.name ='nametest'。但它不工作。请给我一个简单的例子。谢谢朋友

I'm doing project in nodejs and html .can anybody help how to set value to text field in html from server.js. For example I've text field with id 'name' on index.html. i use res.body.name = 'nametest' .but its not working .please give me a simple example . Thank you friends

推荐答案

要从服务器设置字段,您希望将其设置为您在定义时设置的预设值发送HTML或您想稍后动态设置它。第一个选项很简单,第二个选项有点高级。

In order to set a field from the server, you want to make it a preset value which you define when sending the HTML or you want to set it dynamically later on. The first option is easy, the second one a bit advanced.

让我们看看选项1.这只是一个非常基本的例子!请不要在生产中使用它。

Let's look at option 1. This is just a very basic example! Please don't use this in production.

index.html

<!DOCTYPE html>
<html>
  <head><!-- your stuff here --></head>
  <body><input type="text" name="someVal" value="{{someVal}}"></body>
</html>

这可能是您的HTML。只是一个独特的占位符,你想要你的价值去。可能有更好的技术可以做到这一点,但为了简单起见,我选择了这种方式。

This might be your HTML. Just but a distinctive placeholder where you want your value to go. There might be better techniques out there to do this, but for the sake of simplicity I chose this way.

server.js

var http = require('http');
var fs = require('fs');

http.createServer((req, res) => {
  fs.readFile('index.html', (err, data) => {
    if (err) {
      res.writeHead(500);
      res.end(err);
      return;
    }

    data = data.toString().replace(/\{\{someVal\}\}/, 'your value here');
    res.writeHead(200);
    res.end(data, 'utf8');
  });
}).listen(8080);

此server.js将在端口8080上打开HTTP服务器。它将尝试读取<$ c来自同一目录的$ c> index.html 。如果失败,它会将错误消息发送给客户端,否则它将用您的值替换HTML中的占位符,然后将修改后的内容发送给客户端。

如果这就是你要做的全部,PHP可能会为你做得更好(但这只是我的2美分:) :)

This server.js will open a HTTP server on port 8080. It will try to read index.html from the same directory. If it fails, it will send the error message to the client, else it will replace your placeholder in your HTML with your value and then send the modified content to the client.
If that's all you want to do, PHP might do a better job for you (but that's just my 2 cents :) )

选项2更精细。您必须使用AJAJ(异步Javascript和JSON ),这需要客户知道何时获取值或您可以使用websockets,使服务器能够将值推送到客户端。对于其中任何一个,有很多教程比我在这里为你提供的任何内容都要详细得多。

如果你想使用这些技术,但对它们有点不确定实现,你可能想看看像Meteor和Socket.IO这样的框架

Option 2 is a lot more elaborate. You would have to either use AJAJ (Asynchronous Javascript and JSON) which requires the client to know when to fetch the value or you could make use of websockets which enable the server to push a value to the client. For either of those there are a lot of tutorials out there which are a lot more detailed than anything I could put together for you here.
If you want to use those techniques, but are a bit unsure about their iomplementation, you might want to look at frameworks like Meteor and Socket.IO

这篇关于如何在nodejs中设置html中的文本字段值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆