gpt4 book ai didi

javascript - app.get ('env' )在本地主机和生产中的使用

转载 作者:太空宇宙 更新时间:2023-11-04 03:06:53 27 4
gpt4 key购买 nike

var express = require('express');
var app = express();
var db_url;
if(app.get('env') == "development"){
db_url = 'mongodb://127.0.0.1:27017/localhost';
}else{
db_url = 'something else';
}

console.log(app.get('env'));

express 中的 app.get('env') 是什么意思?当我将代码部署到实时服务器时,我看到它仍然打印 development

最佳答案

你需要告诉它你正处于生产模式;请参阅part of the Express docs .

In development, you typically set environment variables in your interactive shell, for example by using export or your .bash_profile file. But in general you shouldn’t do that on a production server; instead, use your OS’s init system (systemd or Upstart). The next section provides more details about using your init system in general, but setting NODE_ENV is so important for performance (and easy to do), that it’s highlighted here.

With Upstart, use the env keyword in your job file. For example:

# /etc/init/env.conf
env NODE_ENV=production

For more information, see the Upstart Intro, Cookbook and Best Practices.

With systemd, use the Environment directive in your unit file. For example:

# /etc/systemd/system/myservice.service
Environment=NODE_ENV=production

For more information, see Using Environment Variables In systemd Units.

If you are using StrongLoop Process Manager, you can also set the environment variable when you install StrongLoop PM as a service.

如果需要,您还可以在 JavaScript 文件中设置 process.env.NODE_ENV

关于javascript - app.get ('env' )在本地主机和生产中的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37628374/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com