How to use the port function from yargs

Find comprehensive JavaScript yargs.port code examples handpicked from public code repositorys.

yargs.port is a method that can be used to configure a command line argument for specifying a port number.

24
25
26
27
28
29
30
31
32
33
}

// determine port to be used, the default is port 3000
var port = 3000;
if(argv.port) {
        port = argv.port;
}

http.listen(port, function(){
        console.log('listening on *:' + port);
fork icon2
star icon5
watch icon6

3
4
5
6
7
8
9
10
11
12
13
14
const app = express();
const argv = require('yargs').argv;


// verificar si se recibio port por parametro
if (argv.port != undefined && Number.isInteger(argv.port)) {
    expressClientPort = argv.port;
}


app.use(express.static(__dirname + "/public"));

fork icon0
star icon0
watch icon1

+ 20 other calls in file

How does yargs.port work?

yargs.port is an API in the Yargs library for defining a command-line option for a port number, allowing users to specify a port number as an input to the command-line interface. When using the yargs.port method, the argument for the option can be a boolean or a string, and Yargs will automatically parse the user input as a port number.

27
28
29
30
31
32
33
34
35
36
},options, argv]);

options.devtools = true;

if(argv.port !== undefined && argv.port > 80) {
    options.port = argv.port;
}

/*
 * Tor proxy benutzen
fork icon0
star icon0
watch icon1

+ 10 other calls in file

Ai Example

1
2
3
const argv = require("yargs").number("port").argv;

console.log(argv.port);

This will allow you to pass a port number to your script as an argument, like --port 8080. The value of argv.port will be a number, or NaN if no port number was provided.

Other functions in yargs

Sorted by popularity

function icon

yargs.argv is the most popular function in yargs (1012 examples)