The content of this article is about how to pass parameters (code) using the command line in Nodejs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Use commander.js.
npm install commander
Then introduce commander.js into the code and use it directly.
Tips:
If it is just -p --port, without [type], the passed in is a Boolean value, true/false.
-p, -- port [type], pay attention to the space in the middle.
Go directly to the code
let express = require('express');let program = require('commander'); let port = 3000 // 设置默认端口 let app = express() //启动一个web服务器 app.set('views','./views'); //设置视图的根目录 app.set('view engine', 'jade'); // 设置视图模板引擎 app.listen(port) program .option('-p, --port [type]','Add port') .parse(process.argv); if (program.port) port = program.port; console.log('imovie start on port '+ port);
Related recommendations:
nodejs command line parameter processing module commander usage example_node.js
nodejs npm common command collection
The above is the detailed content of How to pass parameters using the command line in Nodejs (code). For more information, please follow other related articles on the PHP Chinese website!