javascript NodeJS 服务器, res.sendfile 返回 HTML 但不返回“jscript 包含”(<script src>)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24410340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NodeJS server, res.sendfile returning HTML but not the "jscript includes" (<script src>)
提问by prk
Basically I want to make a server and then a simple javascript site with phaser to try some stuff, but the html stuff shows, but not the javascript.
基本上我想制作一个服务器,然后是一个简单的带有移相器的 javascript 站点来尝试一些东西,但是显示的是 html 的东西,而不是 javascript。
Here are my different files & codes:
这是我的不同文件和代码:
index.html:
索引.html:
<!doctype html>
<html>
<center>
<body>
test
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="phaser.min.js"></script>
<script src="game.js"></script>
<div id='game'></div>
</body>
</center>
</html>
game.js:
游戏.js:
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'game', { preload: preload, create: create, update: update });
function preload() {
game.load.image('char', 'char.png');
}
var sprite;
var cursors;
function create() {
game.physics.startSystem(Phaser.Physics.P2JS);
game.physics.p2.defaultRestitution = 0.8;
sprite = game.add.sprite(200, 200, 'char');
game.physics.p2.enable(sprite);
sprite.body.setZeroDamping();
sprite.body.fixedRotation = true;
text = game.add.text(20, 20, 'l2arrowkeys', { fill: '#ffffff' });
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
sprite.body.setZeroVelocity();
if (cursors.left.isDown)
{
sprite.body.moveLeft(200);
}
else if (cursors.right.isDown)
{
sprite.body.moveRight(200);
}
if (cursors.up.isDown)
{
sprite.body.moveUp(200);
}
else if (cursors.down.isDown)
{
sprite.body.moveDown(200);
}
}
server.js:
服务器.js:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendfile('index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('a user disconnected');
});
});
http.listen(1337, function(){
console.log('listening on *:1337');
});
Basically the game.js shows up if I only open the index.html in a browser, but not if I do 'node server.js' and then go to localhost:1337, then it would only show the 'test' (plain text), but not the Javascript. I wasn't sure what I should have put as the title, sorry if it's misleading/makes no sense.
基本上,如果我只在浏览器中打开 index.html,game.js 就会显示出来,但如果我执行“node server.js”然后转到 localhost:1337,则它不会显示“test”(纯文本) ),但不是 Javascript。我不确定我应该把什么作为标题,抱歉,如果它具有误导性/没有意义。
回答by mscdex
You need to include a static file serving middleware at the top of your stack:
您需要在堆栈顶部包含一个静态文件服务中间件:
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
res.sendfile('index.html');
});
// ...
So if you put your js files in a subdirectory named 'public' (relative to where the script is), the browser should be able to access the javascript files.
因此,如果您将 js 文件放在名为“public”的子目录中(相对于脚本所在的位置),浏览器应该能够访问 javascript 文件。
回答by Diego
You're not configuring well express. The static middleware handles serving up the content from a directory. In this case the 'public' directory is served up and any content (HTML, CSS, JavaScript) will be available. Will need the (express.static(__dirname + '/app'));on your express configuration
你没有配置好快递。静态中间件处理提供目录中的内容。在这种情况下,将提供“public”目录,并且任何内容(HTML、CSS、JavaScript)都将可用。将需要(express.static(__dirname + '/app')); 在您的快速配置上
app.configure(function () {
app.use(express.logger());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/app'));
app.set('PORT', 3000);
});
回答by Milad Rezazadeh
I had the same issue it was because of Express version, I upgrade it to the 4.x version, and I started to get different error (which was good) and then I used below command :
我遇到了同样的问题,因为 Express 版本,我将它升级到 4.x 版本,然后我开始收到不同的错误(这很好),然后我使用了以下命令:
app.get('/home', function (req, res) {
res.sendFile( __dirname + "/public/" + "index.html" );
})
and it worked !
它奏效了!
回答by lm5050
I had the same issue with sendfile returning the HTML but not the < script src >
我有同样的问题,sendfile 返回 HTML 但不是 < script src >
res.sendFile(path.join(__dirname+'/myFile/index.html'))
Which was fixed by adding the static folder containing the script
这是通过添加包含脚本的静态文件夹来修复的
app.use(express.static(path.join(__dirname, 'myFile')));