将Node.js开发的Autodesk Forge Web应用打包为.exe及部署问题
问题背景
我有一个可用的Autodesk Forge应用(包含BIM360 Hub侧边栏、Forge Viewer及部分图表),目前仅能在Visual Studio Code IDE中运行。我希望将该应用打包为.exe文件,以便分发给用户、部署到搭载IIS的服务器等。
基本信息
- 参考Petr Broz的教程搭建了Viewer和Hub的后端
- 应用基于Node.js运行
尝试操作及问题
我使用nexe模块构建可执行文件,指定start.js作为入口点并定义nexe.config.js文件,成功生成exe文件,但从命令行运行时出现错误:
Missing FORGE_CLIENT_ID or FORGE_CLIENT_SECRET env. variables.
尽管我已在config.js中配置了相关变量。
核心问题
- 有没有其他方法可以从Visual Studio Code将Forge Web应用打包为.exe文件?
- 我使用nexe的过程中是否存在操作错误?
- 是否可以通过.exe文件将Web应用部署到IIS?现有文档均指向Azure、AWS和Heroku。
相关文件
start.js
const path = require('path');//bringing in built in node js modeules ( to resulve file system path ) const express = require('express');//module to create the express server const cookieSession = require('cookie-session'); //any piece of code would have an opportunity to handle the request const PORT = process.env.PORT || 3000; const config = require('./config.js'); if (config.credentials.client_id == null || config.credentials.client_secret == null) { console.error('Missing FORGE_CLIENT_ID or FORGE_CLIENT_SECRET env. variables.'); return; } let app = express(); //static middlewere to check for the front end files (html,js,css) app.use(express.static(path.join(__dirname, 'public')));//method inside express module: a middlewere for serving static files this line will check in 'public' folder if the request //that is sent (specific file) is in there. if so - it will ignore the rest of the stack(the rest of the code) app.use(cookieSession({ // create 2 cookies that stores the name and encrypted key name: 'forge_session', keys: ['forge_secure_key'],//takes cater of decipher the encryption for the forge key for us maxAge: 14 * 24 * 60 * 60 * 1000 // 14 days, same as refresh token })); app.use(express.json({ limit: '50mb' }));//middlewere that looks at the title of the request - and if its .json it will look at the body of the request and parese it to javascript object app.use('/api/forge', require('./routes/oauth.js'));//adding our custom express routers that will handle the different endpoints. app.use('/api/forge', require('./routes/datamanagement.js')); app.use('/api/forge', require('./routes/user.js')); app.use((err, req, res, next) => { console.error(err); res.status(err.statusCode).json(err); }); app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); });
config.js
// Autodesk Forge configuration module.exports = { // Set environment variables or hard-code here credentials: { client_id: process.env.FORGE_CLIENT_ID, client_secret: process.env.FORGE_CLIENT_SECRET, callback_url: process.env.FORGE_CALLBACK_URL }, scopes: { // Required scopes for the server-side application-->privileges for our internal operation in the server side ("back end") internal: ['bucket:create', 'bucket:read', 'data:read', 'data:create', 'data:write'], // Required scope for the client-side viewer-->privileges for the client ("front end") public: ['viewables:read'] } };
解决方案
1. 其他打包为.exe的方法
除nexe外,还有几种成熟工具可将Node.js应用打包为exe:
- pkg:轻量命令行工具,直接将Node.js运行时、代码及依赖打包成单个可执行文件。使用示例:
pkg start.js --targets node18-win-x64 - Electron:适合将Web应用封装为带桌面窗口的应用,内置Chromium环境,缺点是打包体积较大。
- node-packer:多平台支持的轻量打包工具,操作门槛低。
2. nexe使用中的问题排查
你遇到的环境变量缺失错误,核心原因是nexe打包时不会自动注入process.env变量,而你的config.js依赖该对象读取密钥。
常见失误及修复方式:
- 未注入环境变量:打包时需显式传递变量,或直接在config.js中硬编码(注意保密,分发时避免泄露密钥):
- 方式一:命令行传递:
nexe start.js --env FORGE_CLIENT_ID=你的ID --env FORGE_CLIENT_SECRET=你的密钥 - 方式二:修改config.js:
credentials: { client_id: '你的实际Client ID', client_secret: '你的实际Client Secret', callback_url: '你的回调地址' }
- 方式一:命令行传递:
- 资源文件未打包:检查nexe.config.js是否配置了静态资源路径,确保public目录下的前端文件被包含:
module.exports = { input: 'start.js', output: 'forge-app.exe', resources: ['./public/**/*'], env: { FORGE_CLIENT_ID: '你的ID', FORGE_CLIENT_SECRET: '你的密钥', FORGE_CALLBACK_URL: '你的回调地址' } };
3. 通过.exe部署到IIS的可行性
可以通过.exe部署到IIS,推荐用反向代理的方式实现:
- 将exe注册为Windows服务(可借助NSSM工具),确保它能后台运行并监听固定端口(如3000)。
- 在IIS中安装URL Rewrite和**Application Request Routing (ARR)**模块,开启反向代理功能。
- 创建新网站,配置URL重写规则,将所有请求转发到
http://localhost:3000。 - 配置Windows防火墙允许该端口的入站请求。
也可尝试用iisnode模块托管,但该方式更适合直接运行Node.js代码,而非exe文件,反向代理方案更简单可靠。
内容的提问来源于stack exchange,提问作者levi
相关产品推荐
相关产品推荐

