Search code examples
javascriptnode.jsexpresssocket.iocreatejs

node.js, Get issue


I am using node.js to create a server game. But I have a problem which I don't know how to solve. I will be grateful for any help.

Here is the problem:

enter image description here

index.js:

var express = require('express');
var app = express();
var http = require('http').createServer(app);
var io = require('socket.io').listen(http);

http.listen(8080);
io.set('log level', 1);

app.get('/', function(req, res) {
    console.log("The path to your index.js is " + __dirname);
    res.sendfile(__dirname + '/' + "index.html");
});

index.html:

<html>
</head>
    <title>Server Game</title>
</head>
<body>
    <canvas id = "map" width = "800px" height = "800px"></canvas>

    <script src="scripts/createjs-2013.12.12.min.js"></script>
    <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>

    <script src="/socket.io/socket.io.js"></script>
    <script src="scripts/script.js"></script>
</body>

script.js:

var stage;
var queue;

window.addEventListener('load', init);

function init() {
    stage = new createjs.Stage("map");
    queue= new createjs.LoadQueue(true);
    queue.addEventListener("complete", handleComplete);
    queue.loadManifest([{id:"hud", src:"gui/ui/hud.png"}]);

   createjs.Ticker.addEventListener("tick", tickHandler);
}

function handleComplete(e) {
    var bg = new createjs.Bitmap(queue.getResult("hud"));
    stage.addChild(bg);
}

function tickHandler(e) {
    stage.update();
}

Thanks for attention!


Solution

  • You need to register your public directory with express.static() otherwise your static assets won't be served by your express app.

    Add this to your index.js before your routes

    app.use(express.static('public'));

    Obviously, your static directory doesn't need to be public so just swap that with whatever your static directory is.