Node.js无法识别我的server.js路径

时间:2018-12-27 01:51:43

标签: node.js express heroku path

我正在尝试在主目录C:\Users \THIAGOSAAD\Documents\DEVELOPMENT\NEORIS\ALIANSCE\aliansce-app-analyticals-panel内运行yarn start_dev命令

但是,这样做时,它开始在我的“ process.env.DATABASE_URL”中显示错误,好像无法识别“ dotenv”文件夹中的“ environment”一样!

consign v0.1.6 Initialized in C:\Users\THIAGOSAAD\Documents\ DEVELOPMENT\NEORIS\ALIANSCE\aliansce-app-analyticals-panel

C:\Users\THIAGOSAAD\Documents\DEVELOPMENT\NEORIS\ALIANSCE\aliansce-app-analyticals-panel\node_modules\mongoose\lib\connection.js: 428
    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 the string.
    at new MongooseError (C: \Users\THIAGOSAAD\Documents\DEVELOPMENT\ NEORIS\ALIANSCE\aliansce-app-analyticals-panel\node_modules\mongoose\lib\ error\mongooseError.js: 13:11)
    at NativeConnection.Connection.openUri (C:\Users\THIAGOSAAD\Documents\ DEVELOPMENT\NEORIS\ALIANSCE\aliansce-app-analyticals-panel\node_modules\ mongoose\lib\connection.js: 428:11)
    at Mongoose.connect (C:\Users\THIAGOSAAD\Documents\DEVELOPMENT\ NEORIS \ ALIANSCE\aliansce-app-analyticals-panel\node_modules\mongoose\lib\ index.js: 270: 15)

如果我直接在server.js上输入网址,则会开始在寄售商品中显示错误:

consign v0.1.6 Initialized in C:\Users\THIAGOSAAD\Documents\DEVELOPMENT \NEORIS\ALIANSCE\aliansce-app-analyticals-panel
! Entity not found C:\Users\THIAGOSAAD\Documents\DEVELOPMENT\NEORIS\ALIANSCE\aliansce-app-analyticals-panel\routes
! Entity not found C:\Users\THIAGOSAAD\Documents\DEVELOPMENT\NEORIS\ ALIANSCE \ aliansce-app-analyticals-panel\controllers
SERVER ROLLING IN PORT 3000: ')

但是,如果我从start.js所在的目录中运行nodemon,它将起作用:C: \Users\THIAGOSAAD\Documents\DEVELOPMENT\NEORIS\ALIANSCE\aliansce-app-analyticals-panel\app\backend\src\start.js

我想知道为什么会这样以及如何解决它,因为我试图将服务器上载到Heroku且它没有运行,可能是因为运行yarn start_dev时遇到了同样的问题! / strong>

我的目录 enter image description here

SERVER.JS

const express  = require('express');
const dotenv   = require('dotenv');
const morgan   = require('morgan');
const consign  = require('consign');
const mongoose = require('mongoose');
const nunjucks = require('nunjucks');

class Application {
    constructor(){
        this.express  = express();
        this.dotenv   = dotenv.config({ path: './environment/.env' });
        this.morgan   = morgan('dev');
        this.consign  = consign();
        this.mongoose = mongoose;
        this.nunjucks = nunjucks;

        // INICIALIZA AS FUNÇÕES AUTOMATICAMENTE
        this.initDatabase();
        this.initMiddlewares();
        this.initConsign();
        this.initViews();

    }

    initDatabase(){ 
        this.mongoose.connect(process.env.DATABASE_URL, {
            useCreateIndex:  true,
            useNewUrlParser: true
        });
    }

    initMiddlewares(){
        this.express.set('view engine', 'njk');

        this.express.use(express.static('../../app/frontend'));
        this.express.use(express.json());
        this.express.use(express.urlencoded({ extended: true }));
        this.express.use(this.morgan);
    }

    initConsign(){
        this.consign.include('./routes').then('./controllers').into(this.express);
    }

    initViews(){
        this.nunjucks.configure('./views/pages', {
            watch:      true, // SOMENTE PARA DESENVOLVIMENTO
            express:    this.express,
            autoescape: true
        });
    }

}

module.exports = new Application().express;

PACKAGE.JSON

{
  "author": "THIAGO DE BONIS CARVALHO SAAD SAUD",
  "scripts": {
    "start_dev": "nodemon app/backend/src/start.js"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "bootstrap": "^4.2.1",
    "bootstrap-select": "^1.13.5",
    "chartjs": "^0.3.24",
    "consign": "^0.1.6",
    "dotenv": "^6.2.0",
    "express": "^4.16.4",
    "express-validator": "^5.3.1",
    "jquery": "^3.3.1",
    "jsonwebtoken": "^8.4.0",
    "mongoose": "^5.4.1",
    "nunjucks": "^3.1.6"
  },
  "devDependencies": {
    "morgan": "^1.9.1",
    "nodemon": "^1.18.9"
  }
}

1 个答案:

答案 0 :(得分:0)

设置dotenv文件夹路径时,请设置绝对路径。

this.dotenv   = dotenv.config({ path: require('path').join(__dirname, './environment') });