node.js,获取问题
node.js, Get issue
我正在使用 node.js 创建服务器游戏。但是我有一个问题,我不知道如何解决。我将不胜感激任何帮助。
这是问题所在:
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();
}
感谢关注!
您需要使用 express.static()
注册您的 public
目录,否则您的 Express 应用程序将无法提供您的静态资产。
将此添加到您的 index.js 路线之前
app.use(express.static('public'));
显然,您的静态目录不必是 public
,因此只需将其与您的静态目录交换即可。
我正在使用 node.js 创建服务器游戏。但是我有一个问题,我不知道如何解决。我将不胜感激任何帮助。
这是问题所在:
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();
}
感谢关注!
您需要使用 express.static()
注册您的 public
目录,否则您的 Express 应用程序将无法提供您的静态资产。
将此添加到您的 index.js 路线之前
app.use(express.static('public'));
显然,您的静态目录不必是 public
,因此只需将其与您的静态目录交换即可。