预格式化文本将缩进 4 格 //这里替换HTML URL 地址 须 index.html 结尾
const express = require(“express”);
const app = express();
const runCocos = () => {
const URL = “http://127.0.0.1:3000/static/index.html”
const { JSDOM, ResourceLoader } = require('jsdom')
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
const WebGL = require('gl');
const WebGLRenderingContext = WebGL.WebGLRenderingContext;
WebGLRenderingContext.prototype.texSubImage2D = function () { };
const gl = WebGL(64, 64, { preserveDrawingBuffer: true });
const resourceLoader = new ResourceLoader({
proxy: URL,
strictSSL: false,
userAgent: "Mellblomenator/9000",
});
fetch(URL).then(res => res.text()).then(html => {
console.log("HTML Index 文件: ", html)
global.window = (new JSDOM(html, {
url: URL,
referrer: URL,
contentType: "text/html",
resources: resourceLoader,
storageQuota: 10000000,
runScripts: "dangerously"
})).window;
let HGContext = window.HTMLCanvasElement.prototype.getContext;
window.TextDecoder = global.TextDecoder;
window.HTMLCanvasElement.prototype.getContext = function (type, data) {
if (type == "2d") return HGContext.bind(this)(type, data);
return gl;
};
window.requestAnimationFrame = global.requestAnimationFrame;
window.fetch = fetch;
})
setTimeout(() => { }, 99999999);
}
app.use(’/static’, express.static(‘public’))
app.listen(3000, () => {
console.log(“服务启动”);
runCocos();
}); 改造了下加载服务端本地静态文件,静态文件cocos creator 打包的产物






