0

Express サーバー用の Node.js と websockets 用の Socket.io を使用して、マルチプレイヤー HTML5 ゲームを開発しています。アプリケーションがリッスンしているポートにブラウザでアクセスしようとすると、空白のページが表示されます。ただし、index.html ファイルに (サーバーの実行中に) 直接アクセスすると、完全に機能します。

「ファイルに直接アクセスする」というのは、 file:///C:/Program%20Files/nodejs/SupermarketChallenge/public/index.html を意味することに注意してください。

これは、プレイするためにローカルホスト経由でアクセスする必要がある同居人と一緒にゲームをテストするまでは問題ありませんでした。

関連するサーバーコードは次のとおりです。

var express = require('express'),
    //Create an express server
    app = express.createServer(),
    //attach socket.io to the server
    supermarket = require('socket.io').listen(app);

//Configure a static directory for public files
app.configure(function() {
    app.use(express.static(__dirname + '/public'));
});

//set the port to listen to
app.listen(3000);

そしてクライアント:

var socket = io.connect('http://localhost:3000'); 
4

2 に答える 2

0

これは、going tohttp://localhost:3000/がパブリック フォルダーの有効なファイルと一致しないためです。これに合わせたルートが必要です。次に従ってコードを更新します。

var express = require('express'),
    //Create an express server
    app = express.createServer(),
    //attach socket.io to the server
    supermarket = require('socket.io').listen(app);

//Routes
app.get('/', function(req, res) {
    res.redirect('/index.html');
});

//Configure a static directory for public files
app.configure(function() {
    app.use(app.router);
    app.use(express.static(__dirname + '/public'));
});

//set the port to listen to
app.listen(3000);

これで、実際に のコンテンツを提供するようにルートを変更できます。index.htmlその場合、/ルートを次のように置き換えます。

//Routes
app.get('/', function(req, res) {
    res.sendfile(__dirname + '/public/index.html');
});
于 2012-11-16T16:04:36.830 に答える