Advertisement

Serving html file for webgame.

Started by March 28, 2015 08:18 PM
2 comments, last by Fl00Fy 9 years, 9 months ago

Hello

I've just started learning JavaScript and trying to create a multiplayer webgame using Node.js. I decided to use Amazon's web services to store the website and run the servers, but I can't figure out what exactly to do in order to load the website when a user goes to the url. I'm using Amazon's Elastic Beanstalk service to host the game, which includes storage for my html and js files, and compute servers for running a server when there are players. As far as I have been able to find out, when I connect to the site, one of my js files are run (either app.js or server.js). In order to load the website I need to send the user the html.

Unfortunately, I can't manage to properly send the html file without errors, because the html file links some scripts which are then not found because they haven't been sent.

I've never worked with servers before so I won't be surprised if the answer is embarrassingly obvious. There are 3 different areas in which I could use the help right now:

  • Properly serving an html file to a user along with the necessary scripts
  • Properly using Amazon's Elastic Beanstalk for hosting a website/webgame
  • More appropriate suggestions for hosting than Elastic Beanstalk

Currently this is how I am trying to send the html file to the user (called index.js)


var express = require('express');
var app = express();

app.get('/', function(req, res){
    res.sendFile(__dirname + '/Views/index.html');
});

var server = app.listen(3000, function () {

    var host = server.address().address;
    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);
})

And this is the html file containing the website and the entry point of the game


<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Phaser - Tutorial</title>
    <script type="text/javascript" src="../Scripts/phaser.min.js"></script>
    <script type="text/javascript" src="../Scripts/States/Boot.js"></script>
    <script type="text/javascript" src="../Scripts/States/Preloader.js"></script>
    <script type="text/javascript" src="../Scripts/States/MainMenu.js"></script>
    <script type="text/javascript" src="../Scripts/States/Game.js"></script>
    <script type="text/javascript" src="../Scripts/States/Options.js"></script>
    <script type="text/javascript" src="../Scripts/States/HighScore.js"></script>
    <script type="text/javascript" src="../Scripts/States/Login.js"></script>
    <script type="text/javascript" src="../Scripts/States/LoginSuccessful.js"></script>
    <script type="text/javascript" src="../Scripts/States/LoginUnsuccessful.js"></script>
    <script type="text/javascript" src="../Scripts/States/LevelComplete.js"></script>
    <script type="text/javascript" src="../Scripts/States/GameOver.js"></script>
    <style type="text/css">
        body {
            margin: 0;
        }
    </style>
</head>
<body>
<script type="text/javascript">

    (function() {
        // initialize the framework
        var game = new Phaser.Game(800, 600, Phaser.AUTO, 'game');
        // add game states
        game.state.add('Boot', Demo.Boot);
        game.state.add('Preloader', Demo.Preloader);
        game.state.add('MainMenu', Demo.MainMenu);
        game.state.add('Game', Demo.Game);
        game.state.add('Options', Demo.Options);
        game.state.add('HighScore', Demo.HighScore);
        game.state.add('Login', Demo.Login);
        game.state.add('LoginSuccessful', Demo.LoginSuccessful);
        game.state.add('LoginUnsuccessful', Demo.LoginUnsuccessful);
        game.state.add('LevelComplete', Demo.LevelComplete);
        game.state.add('GameOver', Demo.GameOver);
        // start the Boot state
        game.state.start('Boot');
    })();

</script>

</body>
</html>

And currently my project file structure is as follows:

  • root
    • index.js
    • Views
      • index.html
    • Scripts
      • States
        • ...
      • phaser.min.js

Any help on any at all would be greatly appreciated.

Thanks!

You're links specify that you should first go up a directory and then go into scripts. This means that you'll go up a level from your root directory and then try and access 'Scripts'. Try removing the '../' in front of your links and that should work!

Game Development Tutorials - My new site that tries to teach LWJGL 3.0 and OpenGL to anyone willing to learn a little.

Advertisement

I tried removing the '../' but unfortunately I'm still getting the same error: "Failed to load resource: the server responded with a status of 404 (not found)".

I was under the impression that it would look for the scripts from where the html file that is trying to link them is. In this case the html file is in Views, and would need to go up one level in order to get into Scripts. Or does it look from the root directory regardless of which file is searching?

I managed to get it working using express.static middleware to serve all the scripts and resources, by adding the following lines:


app.use('/Scripts', express.static(__dirname + '/Scripts'));
app.use('/Images', express.static(__dirname + '/Images'));
app.use('/Sounds', express.static(__dirname + '/Sounds'));

I tried using a '/public' folder to do this but I couldn't get it working. I'll probably play around with it to find out why as I think it would be cleaner.

Thanks for your help!

This topic is closed to new replies.

Advertisement