我正在尝试使用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);
答案 0 :(得分:1)
要使用Foreman,您需要在项目的根目录中创建一个名为Procfile
的文件 - 该文件告诉领班如何运行您的项目。
在此文件中,您可能希望添加如下所示的行:
web: node server.js
或者,无论使用什么命令来“启动”您的Web服务。然后,如果从命令行运行foreman start
,foreman将解析Procfile,然后为您运行该命令(node server.js
)。