简体   繁体   English

node.js和HTML页面? 如何结合?

[英]node.js and HTML page? How to combine?

I would like to establish a communication between a HTML-page with some JavaScript code and a simple webserver based on node.js. 我想在带有一些JavaScript代码的HTML页面和基于node.js的简单Web服务器之间建立通信。 My first node program (demo) looks like this and i see it working by calling http:…name=someName from the web browser! 我的第一个节点程序(演示)看起来像这样,我看到它通过从Web浏览器中调用http:…name = someName起作用! Fantastic! 太棒了!

http = require('http');
var url = require('url');
var htmlencode = require('htmlencode');
var port = process.env.PORT || 3000;

var request = function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
var parsed_url = url.parse(req.url, true)
res.write("");
res.write("<h1>Hello World</h1>");
res.write("<h2>Hello " + htmlencode.htmlEncode(parsed_url.query.name)+"</h2>");
res.write("");
res.end();
};

var server = http.createServer(request);
server.listen(port, function() {
console.log("Call server with http://localhost:" + port +"?name=");
});

Now i have my HTTP-page and i would like to start with that and then let node handle the requests. 现在我有了我的HTTP页面,我想从此开始,然后让节点处理请求。 I assume, i have to load it from node, but how? 我认为,我必须从节点加载它,但是如何? Or do i simply load it by calling the page and then switch to node, but how again? 还是我只是通过调用页面来加载它,然后切换到节点,但是又如何呢? I guess communication from my page is done by AJAX? 我想页面上的交流是由AJAX完成的吗?

Thanks for any help and i would appreciate some sample code! 感谢您的帮助,我将感谢您提供一些示例代码! Peter 彼得

Now i have my HTTP-page and i would like to start with that and then let node handle the requests. 现在我有了我的HTTP页面,我想从此开始,然后让节点处理请求。 I assume, i have to load it from node, but how? 我认为,我必须从节点加载它,但是如何?

You've written an anonymous function and assigned it to a variable called request . 您已经编写了一个匿名函数并将其分配给名为request的变量。 You've set up your program so every time your server gets a request, that function creates a response to it. 您已经设置了程序,因此每当服务器收到请求时,该函数便会对其做出响应。

If you want to respond to requests for your HTML page with your HTML page, then you need to change that function so it responds with the HTML page you want. 如果要用HTML页面响应对HTML页面的请求,则需要更改该功能,以便它以所需的HTML页面响应。

Presumably, that will be writing a condition to check what URL was requested (the details are in the object assigned to req ) and then either do what you are doing now, or read the HTML file and send its contents. 据推测,这将是编写条件来检查请求的URL(详细信息在分配给req的对象中),然后执行您现在正在做的事情,或者读取HTML文件并发送其内容。

Or do i simply load it by calling the page and then switch to node, but how again? 还是我只是通过调用页面来加载它,然后切换到节点,但是又如何呢?

Then you run two HTTP servers and will have to use absolute URLs to reference one from the other. 然后,您运行两个HTTP服务器,并且必须使用绝对URL来相互引用一个。

I guess communication from my page is done by AJAX? 我想页面上的交流是由AJAX完成的吗?

There's no need to involve client-side JavaScript at all. 完全不需要涉及客户端JavaScript。 A regular form submission should do fine. 定期提交表格应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM