【问题标题】:Node, Express, Dot env error in production (Heroku)生产中的 Node、Express、Dot 环境错误(Heroku)
【发布时间】:2022-07-24 13:59:37
【问题描述】:

我在 Heroku 中部署 MERN 应用时遇到了麻烦。该应用程序在本地机器上正常工作,但在生产中,它一次又一次地给我一个错误。

使用命令node Shoping-Backend/server.js启动进程 /app/node_modules/path-to-regexp/index.js:63 path = ('^' + path + (strict ? '' : path[path.length - 1] === '/' ? '?' : '/?')) TypeError: Cannot read properties of undefined (reading 'length')

在我的 app.js 和 server.js 中没有使用 length。我已经在 Heroku 的网站上声明了所有 ENV。但是还是这个错误

版本

  • 节点 v14.17.4
  • express@4.17.2
  • dotenv@14.2.0

Procfile web: node Shoping-Backend/server.js

我的 app.js 文件

const express = require('express');
const app = express();

const fileUpload = require('express-fileupload');
const bodyParser = require('body-parser');
// const dotenv = require('dotenv'); 
const path = require('path');
const cookieParser = require("cookie-parser");
const cors = require('cors');
const morgan = require('morgan');
const errorMiddleware = require('./middlewares/errors/errors');

// setting up config files  // if this then why giving me error
if (process.env.NODE_ENV !== 'PRODUCTION') require('dotenv').config({ path: 'shoping-backend/config/config.env' }); 


app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ limit: "50mb", extended: true }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(fileUpload());



const bassurl = process.env.BASS_URL // api/v1


// emport routes 
const product = require('./route/product');
const userRoute = require('./route/auth');
const orderRoute = require('./route/order');
const paymentRoute = require('./route/payment');


// middlewares 
app.use(morgan('dev'));
// app.use(cors());
app.use(cors({
     origin: 'http://localhost:3000',
     credentials: true,
     // allowedHeaders: 'Origin, X-Requested-With, Content-Type, Accept'
}));
// app.set('trust proxy', 1);

// endpoints 
// app.get('/',(req,res)=>{
//      res.status(200).json({message:'ok find it!!!'})
// });
app.use(bassurl, product);
app.use(bassurl, userRoute);
app.use(bassurl, orderRoute);
app.use(`${bassurl}/payment`, paymentRoute)


// add front static files
// if (process.env.NODE_ENV === 'PRODUCTION') {
app.use(express.static(path.join(__dirname, '../shoping_front/build')));

app.get('*', (req, res) => {
     res.sendFile(path.resolve(__dirname, '../shoping_front/build/index.html'))
})
// }

// error middlewares
app.use(errorMiddleware)

module.exports = app;

我的 server.js 文件

const app = require('./app');
const connectToDatabase = require('./config/database');
const cloudinary = require('cloudinary');

// handle Uncaught exceptions Error (undefind verialbe or values)
process.on('uncaughtException', err => {
     console.log(`ERROR NAME: ${err.name} -> ERROR: ${err.stack}`);
     console.log('Shutting down the server due to Uncaught exceptions (undefind verialbe)');
     process.exit(1);
});
// setting up config files // if this then why giving me error
if (process.env.NODE_ENV !== 'PRODUCTION') require('dotenv').config({ path: 'shoping-backend/config/config.env' });

const PORT = process.env.PORT || 8080;

// Database conection
connectToDatabase();

// config cloudinary
cloudinary.config({
     cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
     api_key: process.env.CLOUDINARY_API_KEY,
     api_secret: process.env.CLOUDINARY_API_SECRET
});

// listen
const server = app.listen(PORT, () => {
     console.log(`server started on PORT ${PORT} as ${process.env.NODE_ENV} mode.`);
});

// handle unhandledRejection Error
process.on('unhandledRejection', err => {
     console.log(`ERROR NAME: ${err.name} -> ERROR: ${err.message}`);
     console.log('Shutting down the server due to Unhandled Promisse rejection');
     server.close(() => process.exit(1));
});   

如果我已经声明了所有 ENV 那么为什么会出现这个错误?

【问题讨论】:

  • 请把表情符号留在家里。他们几乎从来没有帮助过,他们只是分散了你想要问的东西。专注于使您的问题尽可能清晰。没有别的了。
  • path.length 没有出现在您的代码中。请edit您的问题并向我们展示full错误消息,并带有回溯。我们需要知道它的来源。
  • 我使用表情符号只是为了在网上引起注意......错误在哪里上升。当我从应用程序中删除 DotEnv 行时,就会出现此错误。没有path.length

标签: node.js express heroku production-environment dotenv


【解决方案1】:

您的 Procfile 定义有一个到 server.js 文件的路由,您应该改用一个到 app.js 文件的路由。

【讨论】:

    猜你喜欢
    • 2021-03-10
    • 2017-05-03
    • 2018-08-25
    • 2012-11-23
    • 1970-01-01
    • 2020-06-23
    • 2019-10-22
    • 2021-05-04
    • 1970-01-01
    相关资源
    最近更新 更多