我正在尝试使用foreman包来查看我设置我的.env
文件的方式是否可以在我的一个JS文件中使用,但是当我运行nf start server.js
时,我收到以下警告,然后服务器永远不会启动。我认为这与不在server.js文件中调用变量有关,但这并没有改变任何东西
[OKAY] Loaded ENV .env File as KEY=VALUE Format
[WARN] No Procfile Found
[OKAY] package.json file found - trying 'npm start'
[OKAY] Trimming display Output to 55 Columns
[WARN] Required Key 'server.js' Does Not Exist in Procfile Definition
这是我如何使用我的.env
文件:
.ENV
DB_CONFIGURATION=mongodb://<user>:<pw>.mongolab.com:6383219/db-name
blogDB.js:
var DB_CONFIG = process.env.DB_CONFIGURATION;
module.exports = {
url : DB_CONFIG;
}
server.js(我在哪里调用blogDB文件)
//Load express
var express = require('express');
var app = express();
var router = express.Router(); // get an instance of the router
var bodyParser = require('body-parser'); // configure app to use bodyParser()
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session');
app.use(bodyParser.urlencoded({ extended: true})); // get data from a POST method
app.use(bodyParser.json());
app.use(morgan('dev'));
app.use(cookieParser());
var port = process.env.PORT || 8080; // set the port
var blogDB = require('./config/blogDB.js');
mongoose.connect(blogDB.url);
require('./config/passport.js')(passport);
app.set('view engine', 'ejs'); // set ejs as the view engine
app.use(express.static(__dirname + '/public')); // set the public directory
app.use(session({ secret: 'thisisatest' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
var routes = require('./app/routes');
app.use(routes); // use routes.js
app.listen(port);
console.log('magic is happening on port' + port);
要使用Foreman,您需要在项目的根目录中创建一个名为Procfile
的文件 - 该文件告诉工头如何运行您的项目。
在此文件中,您可能希望放置如下所示的行:
web: node server.js
或者,无论使用什么命令来“启动”您的Web服务。然后,如果从命令行运行foreman start
,foreman将解析Procfile,然后为您运行该命令(node server.js
)。