在gulp-nodemon中设置端口有什么用?(What is the use of setting a port in gulp-nodemon?)

我正在使用gulp-nodemon因为它最明显的实用程序。

Nodemon是一个实用程序,它将监视源中的任何更改并自动重新启动服务器。

但我不理解在快递/节点开发中似乎普遍存在的做法。

我刚开始使用node和express,但据我所知:

app.js

var express = require('express'), app = express(), port = process.env.PORT || 8016; app.get('/', function rootHndlr(req, res) { /* body... */ res.send('welcome to my API!'); }); app.listen(port, function listenHndlr(){ console.log('Gulp is running my app on PORT ' + port); });

如果未设置,以下是将端口设置为8016 。

port = process.env.PORT || 8016;

所以现在我们绑定并侦听指定主机和端口上的连接。

但后来我看到人们在他们的nodemon任务中配置以下nodemon到他们的gulpfile.js中的nodemon

gulpfile.js

var gulp = require('gulp'), nodemon = require('gulp-nodemon'); gulp.task('default', function() { // content nodemon({ script: 'app.js', ext: 'js' env: { PORT: 8000 }, ignore: ['./node_modules/**'] }). on('restart', function(){ consile.log('Restarting'); }); });

正如你可以在nodemon env: {PORT: 8000}中nodemon一个值env: {PORT: 8000}为什么要再次设置端口?

谢谢!

I am using gulp-nodemon because of its most obvious of utilities.

Nodemon is a utility that will monitor for any changes in your source and automatically restart your server.

But I am not understanding a practice which seems to be prevalent in express/node development.

I just started working with node and express but from what I understand:

app.js

var express = require('express'), app = express(), port = process.env.PORT || 8016; app.get('/', function rootHndlr(req, res) { /* body... */ res.send('welcome to my API!'); }); app.listen(port, function listenHndlr(){ console.log('Gulp is running my app on PORT ' + port); });

The following is setting in the port to 8016 if not set.

port = process.env.PORT || 8016;

So now we binds and listens for connections on the specified host and port.

But then I see people configure in their gulp tasks the following to nodemon in their gulpfile.js

gulpfile.js

var gulp = require('gulp'), nodemon = require('gulp-nodemon'); gulp.task('default', function() { // content nodemon({ script: 'app.js', ext: 'js' env: { PORT: 8000 }, ignore: ['./node_modules/**'] }). on('restart', function(){ consile.log('Restarting'); }); });

As you can one of the values in nodemon env: {PORT: 8000} Why set the port again?

Thanks!

最满意答案

人们使用类似的东西作为后备: port = process.env.PORT || 8016; port = process.env.PORT || 8016;

您的应用程序应该足够灵活,并通过传递env var使其侦听另一个端口。 一般来说,这是env vars的目的。

关于你的例子,我想有一个原因是写这个gulpfile的人想让应用程序听到端口8000.我会说更改值或删除PORT: 8000是安全的您100%确定应用程序无需在端口8000上运行(例如,它位于将流量转发到端口8000的反向代理之后)。

People are using something like that as a fallback: port = process.env.PORT || 8016;

Your application should be flexible enough and by passing an env var to make it listen to another port. In general, this is the purpose of the env vars.

About your example, I suppose that there is a reason that the guy that wrote this gulpfile would like to make the app listen to port 8000. I would say that it is safe to change the value or to remove the PORT: 8000 as soon as you are 100% sure that there is no reason that the application needs to run on port 8000 (for example, it is behind a reverse proxy that forwards the traffic to port 8000).

更多推荐