nodejs.cn-Node.js-入门教程:Node.js 开发环境与生产环境的区别

ylbtech-nodejs.cn-Node.js-入门教程:Node.js 开发环境与生产环境的区别

 

1.返回顶部
1、

Node.js 开发环境与生产环境的区别

You can have different configurations for production and development environments.

Node.js assumes it‘s always running in a development environment. You can signal Node.js that you are running in production by setting the NODE_ENV=production environment variable.

This is usually done by executing the command

export NODE_ENV=production

in the shell, but it‘s better to put it in your shell configuration file (e.g. .bash_profile with the Bash shell) because otherwise the setting does not persist in case of a system restart.

You can also apply the environment variable by prepending it to your application initialization command:

NODE_ENV=production node app.js

This environment variable is a convention that is widely used in external libraries as well.

Setting the environment to production generally ensures that

  • logging is kept to a minimum, essential level
  • more caching levels take place to optimize performance

For example Pug, the templating library used by Express, compiles in debug mode if NODE_ENV is not set to production. Express views are compiled in every request in development mode, while in production they are cached. There are many more examples.

You can use conditional statements to execute code in different environments:

if (process.env.NODE_ENV === "development") {
  //...
}
if (process.env.NODE_ENV === "production") {
  //...
}
if([‘production‘, ‘staging‘].indexOf(process.env.NODE_ENV) >= 0) {
  //...
})

For example, in an Express app, you can use this to set different error handlers per environment:

if (process.env.NODE_ENV === "development") {
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
})

if (process.env.NODE_ENV === "production") {
  app.use(express.errorHandler())
})
2、
2.返回顶部
 
3.返回顶部
 
4.返回顶部
 
5.返回顶部
1、
2、
 
6.返回顶部
 
nodejs.cn-Node.js-入门教程:Node.js 开发环境与生产环境的区别 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

nodejs.cn-Node.js-入门教程:Node.js 开发环境与生产环境的区别

上一篇:Vue.js中过滤器(filter)的使用


下一篇:什么是HTTP? 什么是超文本? 什么是URL?