简体   繁体   中英

Joining together nodejs html5 and canvas

I've got a very simple html5 game on one side. Opening the game's html file directly with my browser makes the game work just fine. 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).

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). 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.

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. Try out eg this:

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

Then go with browser to

http://localhost:8000/index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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