简体   繁体   English

在gulp-nodemon中设置端口有什么用?

[英]What is the use of setting a port in gulp-nodemon?

I am using gulp-nodemon because of its most obvious of utilities. 我正在使用gulp-nodemon因为它最实用。

Nodemon is a utility that will monitor for any changes in your source and automatically restart your server. Nodemon是一个实用程序,它将监视源中的任何更改并自动重新启动服务器。

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: 我刚刚开始使用node进行表达,但是据我了解:

app.js 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. 如果未设置,则将以下内容设置为8016

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 但是后来我看到人们在他们的nodemon任务中配置以下内容以在nodemon中进行nodemon

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? 您可以在nodemon env: {PORT: 8000}中的值之一env: {PORT: 8000}为什么再次设置端口?

Thanks! 谢谢!

People are using something like that as a fallback: port = process.env.PORT || 8016; 人们使用类似的东西作为后备: port = process.env.PORT || 8016; port = process.env.PORT || 8016;

Your application should be flexible enough and by passing an env var to make it listen to another port. 您的应用程序应该足够灵活,并传递一个env var使其监听另一个端口。 In general, this is the purpose of the env vars. 通常,这是env var的目的。

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). 关于您的示例,我想写这个gulpfile的那个人想让应用监听端口8000是有原因的。我想说,更改该值或尽快删除PORT: 8000是安全的。您100%确保没有任何理由应用程序需要在端口8000上运行(例如,它在将流量转发到端口8000的反向代理之后)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM