Tyl*_*den 3 javascript command-line command-line-arguments node.js
我有一个文件结构,稍后我将为您枚举。我有一个Web服务器,它在按下按钮时启动命令行过程。我想添加使用命令行参数以无头方式运行服务器的选项。这是我应该这样做的方式吗?这是我的项目结构。
/models
/model1
/model2
/model3
/routes
/index
/test
/users
/credentials
/adduser
/views
/same as routes. Route 'test' has no layout.
Run Code Online (Sandbox Code Playgroud)
在索引或“ /”中,我有一个函数,该函数带有多个参数,并通过单击索引页面上的按钮来启动。然后,我们通过“测试/运行”转发,并呈现“索引”视图。该过程继续在终端中运行。我现在将发布该功能的示例。
router.post('/run', ensureAuthenticated, function(req, res){
return res.redirect('/test/running')
});
// Get Homepage
router.get('/running', ensureAuthenticated, function(req, res){
console.log(res.locals.user);
// console.log(app.locals.user);
const var1 = res.locals.user.username;
const var2 = res.locals.user.username;
const var3 = res.locals.user.username;
const var4= res.locals.user.username;
const deets = {
var5,
var6
};
res.render('index');
dosomething(var1, var2, var3, var4, deets);
setInterval(dosomething, 10 * 1000);
})
});
Run Code Online (Sandbox Code Playgroud)
那么你们怎么看?我将如何通过命令行实现var1-6的传递?我将不胜感激从这里的任何帮助。
我现在在Windows上运行,但是目标服务器用于Ubuntu系统。
在node.js中,您可以使用内置process
变量传递CLI参数
举些例子
// test.js
var args = process.argv;
console.log(args[0]); // it will give the node executable path
console.log(args[1]); // it will give current file name
console.log(args[2]); // cli arguments start index
Run Code Online (Sandbox Code Playgroud)
现在运行代码
$ node test.js hello
/usr/bin/node
/home/blackdaemon/test.js
hello
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
885 次 |
最近记录: |