You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Fly.io部署Express/Node/Mongoose应用失败求助

问题排查:Fly.io部署Express/Mongoose应用失败(连接URI未定义)

问题背景

基于Express、Node.js、Mongoose的应用曾成功部署在Fly.io,更换新设备后访问失效。尝试重新部署(含删除原应用、克隆GitHub代码)均失败,应用启动后崩溃。

核心错误日志

[1/1] Checking that e28677eb572286 [app] is up and running Smoke checks for e28677eb572286 failed: the app appears to be crashing Check its logs: here's the last lines below, or run 'fly logs -i e28677eb572286': INFO Starting init (commit: 0b28cec)... INFO Preparing to run:docker-entrypoint.sh npm run start` as root
INFO [fly api proxy] listening at /.fly/api
2023/06/23 20:04:23 listening on [fdaa:2:7b4:a7b:21a1:8e24:3a29:2]:22 (DNS: [fdaa::3]:53)

men_oauth@0.2.0 start
node ./bin/www.js
Fri, 23 Jun 2023 20:04:24 GMT express-session deprecated req.secret; provide secret option at file:/app/server.js:46:3
Warning: connect.session() MemoryStore is not
designed for a production environment, as it will leak
memory, and will not scale past a single process.
Listening on port 8080
/app/node_modules/mongoose/lib/connection.js:768
throw new MongooseError('The uri parameter to openUri() must be a ’ +
^
MongooseError: The uri parameter to openUri() must be a string, got “undefined”. Make sure the first parameter to mongoose.connect() or mongoose.createConnection() is a string.
at _createMongoClient (/app/node_modules/mongoose/lib/connection.js:768:11)
at NativeConnection.openUri (/app/node_modules/mongoose/lib/connection.js:705:29)
at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:406:15)
at file:///app/config/database.js:7:10
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
Node.js v18.15.0
npm notice
npm notice New minor version of npm available! 9.5.0 → 9.7.2
npm notice Changelog: https://github.com/npm/cli/releases/tag/v9.7.2
npm notice Run npm install -g npm@9.7.2 to update!
npm notice
INFO Main child exited normally with code: 1
INFO Starting clean up.
WARN hallpass exited, pid: 232, status: signal: 15 (SIGTERM)
2023/06/23 20:04:25 listening on [fdaa:2:7b4:a7b:21a1:8e24:3a29:2]:22 (DNS: [fdaa::3]:53)
[ 3.130614] reboot: Restarting system
machine did not have a restart policy, defaulting to restart
INFO Starting init (commit: 0b28cec)…
INFO Preparing to run: docker-entrypoint.sh npm run start as root
INFO [fly api proxy] listening at /.fly/api
2023/06/23 20:04:26 listening on [fdaa:2:7b4:a7b:21a1:8e24:3a29:2]:22 (DNS: [fdaa::3]:53)

men_oauth@0.2.0 start
node ./bin/www.js
Fri, 23 Jun 2023 20:04:28 GMT express-session deprecated req.secret; provide secret option at file:/app/server.js:46:3
Warning: connect.session() MemoryStore is not
designed for a production environment, as it will leak
memory, and will not scale past a single process.
Listening on port 8080
/app/node_modules/mongoose/lib/connection.js:768
throw new MongooseError('The uri parameter to openUri() must be a ’ +
^
MongooseError: The uri parameter to openUri() must be a string, got “undefined”. Make sure the first parameter to mongoose.connect() or mongoose.createConnection() is a string.
at _createMongoClient (/app/node_modules/mongoose/lib/connection.js:768:11)
at NativeConnection.openUri (/app/node_modules/mongoose/lib/connection.js:705:29)
at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:406:15)
at file:///app/config/database.js:7:10
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
Node.js v18.15.0
npm notice
npm notice New minor version of npm available! 9.5.0 → 9.7.2
npm notice Changelog: https://github.com/npm/cli/releases/tag/v9.7.2
npm notice Run npm install -g npm@9.7.2 to update!
npm notice
INFO Main child exited normally with code: 1
INFO Starting clean up.
WARN hallpass exited, pid: 232, status: signal: 15 (SIGTERM)
2023/06/23 20:04:28 listening on [fdaa:2:7b4:a7b:21a1:8e24:3a29:2]:22 (DNS: [fdaa::3]:53)
Error: smoke checks for e28677eb572286 failed: the app appears to be crashing

已尝试操作

  • 重新安装Fly.io CLI
  • 升级Fly.io CLI
  • 删除fly.toml文件后重新部署
  • 调整实例数量(fly scale count 1/fly scale count 2)
  • 重新克隆GitHub代码并部署

排查与解决方案

1. 核心问题定位

日志中明确显示MongooseError: The uri parameter to openUri() must be a string, got “undefined”,说明应用启动时无法读取MongoDB连接URI,导致Mongoose连接失败。

2. 环境变量配置检查

  • 本地验证:确认项目根目录的.env文件存在,且包含MONGODB_URI(或代码中使用的变量名),值为有效的MongoDB连接字符串,本地启动应用npm run start验证是否正常连接。
  • Fly.io配置:
    • 执行fly secrets list查看已配置的密钥,确认是否有MongoDB连接URI条目。
    • 若缺失,执行fly secrets set MONGODB_URI="你的MongoDB连接字符串"添加密钥(删除应用会连带删除关联secrets,需重新配置)。
    • 确保代码中使用的环境变量名与Fly.io secrets完全一致(大小写敏感)。

3. 代码逻辑检查

打开/app/config/database.js,确认URI读取逻辑正确:

  • 示例正确代码:
    import mongoose from 'mongoose';
    
    await mongoose.connect(process.env.MONGODB_URI);
    
  • 若本地使用dotenv,需确保生产环境不加载本地.env文件,避免与Fly.io secrets冲突。

4. 额外警告处理

  • express-session秘钥缺失:在express-session配置中添加secret选项,从环境变量读取:
    app.use(session({
      secret: process.env.SESSION_SECRET,
      resave: false,
      saveUninitialized: false
    }))
    
  • MemoryStore生产环境警告:生产环境替换为Redis等持久化存储,避免内存泄漏和无法扩展。

内容的提问来源于stack exchange,提问作者CatLover2291

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 17:44:53