app.listen(port, () => console.log(`Express started in ` +
`${app.get('env')} mode at http://localhost:${port}` +
`; press Ctrl-C to terminate.`))
如果你现在启动应用,就会看到它正在开发模式下运行,这是默认模式。尝试一下让它在
生产模式下运行:
$ export NODE_ENV=production
$ node meadowlark.js
如果使用
Unix/BSD
,就有一种便捷的写法,可以让你仅在这个命令的运行期间修改环境:
$ NODE_ENV=production node meadowlark.js
这会让服务器仅在生产模式下运行,当服务器终止时,
NODE_ENV
环境变量将不会被修改。
我特别喜欢这种便捷的写法,它降低了偶然误设环境变量的可能性,毕竟误设的值未必需
要全部应用。
如果你以生产模式启动
Express
,可能会出现一些警告,提示有些组件不适
用于生产模式。如果你是跟着本书示例操作的,就会发现
connect.session
使用的是内存存储,不适用于生产环境。等到第
13
章我们把它切换为数据
库存储后,这条警告就会消失。
12.2
特定环境的配置
虽然单独修改运行环境好像没有太大作用,但是在生产模式下,
Express
会打印更多的警
告信息(比如通知你某些模块已经淘汰了 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.