Search Unity

NodeJs as Server for hosting WebGL

Discussion in 'Web' started by Korigoth, Jan 7, 2017.

  1. Korigoth

    Korigoth

    Joined:
    Jul 21, 2014
    Posts:
    105
    I'm trying to build a simple nodejs server to test unity3d on a server.

    I tested my empty scene alone and it work pretty well with WebGL.

    after this, i've put the file of WebGL in my NodeJS server and i get this error:

    Invoking error handler due to UnityLoader.js:1
    Uncaught SyntaxError: Unexpected token <

    What is causing this error?

    Here is my nodejs server code:

    Code (JavaScript):
    1. import * as http from 'http';
    2. import * as express from 'express';
    3. import { json, urlencoded } from 'body-parser';
    4. import * as compression from 'compression';
    5. import * as path from 'path';
    6.  
    7. const port: number = process.env.port || 1337;
    8. const app: express.Application = express();
    9. const clientFolder: string = path.join(__dirname, '/client');
    10.  
    11. app.use(json());
    12. app.use(compression());
    13. app.use(urlencoded({ extended: true }));
    14. app.use(express.static(clientFolder));
    15. app.use(defaultRoute);
    16.  
    17. http
    18.     .createServer(app)
    19.     .listen(port);
    20.  
    21. function defaultRoute(req: express.Request, response: express.Response) : void {
    22.     response.sendfile(clientFolder + '/index.html');
    23. }
    EDIT:

    My probleme come from those urls

    dataUrl: "Release/build.data",
    codeUrl: "Release/build.js",
    asmUrl: "Release/build.asm.js",
    memUrl: "Release/build.mem",


    my nodejs return index.html code instead of the files required by unity3d
     
    Last edited: Jan 7, 2017
  2. tuannd92_vn

    tuannd92_vn

    Joined:
    Nov 20, 2015
    Posts:
    1
    The same issue, Unity help us please!
     
  3. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    does it work if you host a development build? if so, you just need to make sure you set the content type headers when hosting a non-development build (which is compressed). See here for more details.
     
  4. varnava

    varnava

    Joined:
    Mar 14, 2013
    Posts:
    6
    Hi Team Have the same issue.
    Any Updates?
     
  5. MidgardDev

    MidgardDev

    Joined:
    Dec 11, 2012
    Posts:
    47
    You should use express static files, you're not serving your client correctly.

    For example, this is a server of my own:

    Code (csharp):
    1.  
    2. var express = require('express');
    3. var compression = require('compression');
    4. var app = express();
    5.  
    6. app.use(compression());
    7. app.use(express.static(__dirname + '/client/'));
    8.  
    9. app.listen(80, function(){
    10.     console.log("webserver listening!");
    11. });
    Doing it this way, you will send a whole folder (the client folder). I think your problem is that your express server isn't sending out all the client files.

    Edit: nevermind you're using the static files, so idk. My way works flawless!
     
    tisfo and harry4951 like this.
  6. argentummanusbeta

    argentummanusbeta

    Joined:
    Apr 11, 2018
    Posts:
    1
  7. theforgot3n1

    theforgot3n1

    Joined:
    Sep 26, 2018
    Posts:
    207
    Serving the whole webgl folder did not work for me

    The error I got was this...

    When I enabled Decompress Fallback, however, it did work. In fact, I got it running on an Elastic Beanstalk server. This was my very simple final code:

    Code (JavaScript):
    1. var express = require('express');
    2. var compression = require('compression');
    3. var app = express();
    4.  
    5. app.use(compression());
    6. app.use(express.static('client'));
    7. app.listen(8080, function(){
    8.     console.log("webserver listening at port: " + 8080);
    9. });
    Hope it helps.
     
    RadianSmile likes this.