Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

How to host unity webGL on node.js?

Discussion in 'Web' started by vemmu, Jul 26, 2022.

  1. vemmu

    vemmu

    Joined:
    May 15, 2022
    Posts:
    8
    I was following this thread https://forum.unity.com/threads/nodejs-as-server-for-hosting-webgl.449904/

    I am trying to get my unity webGL project to show up as static using node.js but terminal is throwing me this error "TypeError: Object prototype may only be an Object or null: /Testit"

    This is my code at the moment:
    Code (JavaScript):
    1. const express = require('express');
    2. var compression  = require('compression');
    3. const app = express();
    4.  
    5. app.use(compression());
    6. app.use(express.static(__dirname,'/Testit'));
    7.  
    8.  
    9. app.listen(8000, () => {
    10. console.log('Server has started!')
    11. });
    Thanks in advance!
     
  2. aromana

    aromana

    Joined:
    Nov 11, 2018
    Posts:
    137
    Line 6 isn't correct. It looks like you meant to call path.join() to create the path name, and pass that to express.static().

    Presumably, you want:
    Code (JavaScript):
    1. const path = require('path')
    2. app.use(express.static(path.join(__dirname, 'Testit')));
    You could probably also do something simpler, but hackier, and create the path with simple string concatenation, like this:

    Code (JavaScript):
    1. app.use(express.static(__dirname + '/Testit'));
     
    vemmu likes this.
  3. vemmu

    vemmu

    Joined:
    May 15, 2022
    Posts:
    8
    Thank you! This fixed it for me
     
    xiangshushu likes this.
  4. kaioduarte

    kaioduarte

    Joined:
    May 20, 2016
    Posts:
    2
    I had some issues with content-type and content-encoding, and I managed to workaround them this way

    Code (JavaScript):
    1. const express = require('express')
    2. const serveStatic = require('serve-static')
    3. const app = express()
    4.  
    5. app.use(serveStatic('<YOUR_FOLDER>', {
    6.   setHeaders: (res, path) => {
    7.     if (path.endsWith('.wasm.br')) {
    8.       res.setHeader('Content-Type', 'application/wasm');
    9.     }
    10.  
    11.     if (path.endsWith('.br')) {
    12.       res.setHeader('Content-Encoding', 'br');
    13.     }
    14.   }
    15. }));
    16.  
    17. app.listen(8000, () => {
    18.   console.log('Server has started!')
    19. });