node.js和HTML页面?如何结合? [英] node.js and HTML page? How to combine?

查看:281
本文介绍了node.js和HTML页面?如何结合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个HTML页面与一些JavaScript代码和一个基于node.js的简单网络服务器之间的通信。
我的第一个节点程序(演示)看起来像这样,我通过在Web浏览器中调用http:... name = someName来看它的工作情况!神奇!

I would like to establish a communication between a HTML-page with some JavaScript code and a simple webserver based on node.js. My first node program (demo) looks like this and i see it working by calling http:…name=someName from the web browser! 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=");
});

现在我有了我的HTTP页面,我想从那开始,然后让节点处理要求。
我假设我必须从节点加载它,但是如何?
或者我只是通过调用页面来加载它,然后切换到节点,但又如何呢?
我想我的页面通信是由AJAX完成的?

Now i have my HTTP-page and i would like to start with that and then let node handle the requests. 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?

感谢您的帮助,我将不胜感激一些示例代码!
Peter

Thanks for any help and i would appreciate some sample code! Peter

推荐答案


现在我有了我的HTTP页面,与此,然后让节点处理请求。我假设,我必须从节点加载它,但怎么做?

Now i have my HTTP-page and i would like to start with that and then let node handle the requests. 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. You've set up your program so every time your server gets a request, that function creates a response to it.

如果要响应HTML页面的请求与您的HTML页面,然后您需要更改该功能,以便它与您想要的HTML页面作出响应。

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.

假设,这将写入一个条件来检查URL是请求(详细信息位于分配给 req 的对象中),然后执行您现在正在执行的操作,或者读取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.


或者我只是通过调用页面加载它,然后切换到节点,但又是如何加载?

Or do i simply load it by calling the page and then switch to node, but how again?

然后运行两个HTTP服务器,并且必须使用绝对URL来引用另一个。

Then you run two HTTP servers and will have to use absolute URLs to reference one from the other.


我想我的页面通信是通过AJAX完成的?

I guess communication from my page is done by AJAX?

根本不需要涉及客户端JavaScript。常规表单提交应该没问题。

There's no need to involve client-side JavaScript at all. A regular form submission should do fine.

这篇关于node.js和HTML页面?如何结合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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