简体   繁体   English

将Node.js HTML5和Canvas结合在一起

[英]Joining together nodejs html5 and canvas

I've got a very simple html5 game on one side. 我的一侧有一个非常简单的html5游戏。 Opening the game's html file directly with my browser makes the game work just fine. 使用我的浏览器直接打开游戏的html文件可以使游戏正常运行。 On the other side, I have an html sever made with node.js that can serve html text normally (tags like < /br> < p> and so on). 另一方面,我有一个由node.js制成的html服务器,可以正常提供html文本(诸如</ br> <p>之类的标签)。

But when I tell the server to display my game as html text priting the whole html file, the canvas fails to render (although the title does). 但是,当我告诉服务器将我的游戏显示为html文本,以整个html文件为代价时,画布无法渲染(尽管标题确实如此)。 So now I'm kind of lost on what's happening and why I'm not able to link those two things together. 所以现在我对正在发生的事情以及为什么无法将这两件事链接在一起感到迷惑。 Here's a simplified version of my server's code: 这是服务器代码的简化版本:

var http = require('http');
var fs = require('fs');
var index = fs.readFileSync('index.html');

http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(index);
    res.end()
}).listen(7777);

So, what am I doing wrong here? 那么,我在这里做错了什么? Should this work normally? 应该正常工作吗? Am I missing something? 我想念什么吗?

It seems just as if the javascript was sent as plain text and the code was never actually executed. 好像javascript是作为纯文本发送的,并且从未真正执行过代码。

Your server serves only the index.html so if that page depends on any other resources it will fail to work as requests to CSS and JS files will fail. 您的服务器仅提供index.html,因此,如果该页面依赖于任何其他资源,它将无法工作,因为对CSS和JS文件的请求将失败。 Try out eg this: 试试这个:

$ npm -g install simplehttpserver
$ simplehttpserver /path/to/your/folder

Then go with browser to 然后使用浏览器转到

http://localhost:8000/index.html

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

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