[英]How do I globally install a Node.js command-line script on the server?
As a prototype, I've written a simple Node.js command-line script which loads a .txt file and outputs the contents.作为原型,我编写了一个简单的Node.js命令行脚本,它加载一个.txt文件并输出内容。 I've read many articles which suggest that Node.js scripts are usually executed via the command-line and should be installed globally , so that's the direction I've taken, even if there are other techniques.
我读过很多文章,这些文章表明 Node.js 脚本通常通过命令行执行并且应该全局安装,所以这就是我所采取的方向,即使还有其他技术。
For the sake of this query, my globally installed command is called my-prototype
.为了这个查询,我全局安装的命令称为
my-prototype
。
This all works fine on the desktop but I intend to host this prototype on a server, using Express.这一切在桌面上运行良好,但我打算使用 Express 在服务器上托管这个原型。
How do I globally install my-prototype
on the server and, if that's possible, will I be able to execute it in the same way as in my local tests, as a child process?:我如何在服务器上全局安装
my-prototype
,如果可能的话,我是否能够以与本地测试相同的方式作为子进程执行它?:
const { execSync } = require('child_process')
const output = execSync(`${process.execPath} my-prototype`)
console.log(output.toString())
I'm not entirely sure that globally installing command-line scripts is for use on a server and wonder whether that's for desktop use only.我不完全确定全局安装命令行脚本是否适用于服务器,并想知道这是否仅适用于桌面使用。
If you need just output the contents , then you do not need a console, or rather: you only need it once, to run nodejs如果您只需要输出内容,那么您就不需要控制台,或者更确切地说:您只需要一次即可运行 nodejs
const fs = require('fs');
const express = require('express');
const app = express();
//
app.get('/', async(req, res)=>{
let fileContains = fs.readFileSync('./package.json', 'utf8');
try{
fileContains = JSON.parse(fileContains);
}catch(e){}
res.json(fileContains);
});
//
app.listen(80);
Also, read a little more about the pm2 module , it's very useful另外,阅读更多关于pm2 模块的信息,它非常有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.