Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS server, res.sendfile returning HTML but not the "jscript includes" (<script src>)

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.

Here are my different files & codes:

index.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:

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:

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.

like image 666
prk Avatar asked Jun 25 '14 13:06

prk


People also ask

What is RES sendFile in node JS?

Mar 13, 2020. Express' sendFile() function lets you send a raw file as a response to an HTTP request. You can think of res. sendFile() as Express' static middleware for a single endpoint.

What does sendFile return?

RETURN VALUES The sendfile() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

Is Res sendFile async?

res. sendFile() is asynchronous and it will end its own response if it is successful.


1 Answers

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.

like image 155
mscdex Avatar answered Nov 03 '22 20:11

mscdex