简体   繁体   English

如何在服务器上全局安装 Node.js 命令行脚本?

[英]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.

相关问题 如何处理 mongo 脚本中的命令行参数? - How do I handle command-line arguments in a mongo script? 如何在全局范围内使用 a.js 文件作为节点命令? - How do I use a .js file as a node command globally? 如何在 node.js 脚本中执行命令行 arguments - How can I execute command line arguments in a node.js script 如何在本地 Node.js 服务器上为 JS 项目运行 npm install 和此类命令? - How do I run npm install and such commands for a JS project on a local Node.js server? 如何使用 NPM 为 node.js 安装/下载提示命令 - How do I install/download the prompt command with NPM for node.js 如何将命令行 arguments 传递给 Node.js 程序? - How do I pass command line arguments to a Node.js program? 如何在远程服务器(ubuntu)上的node.js中安装并连接到mongodb? - How do I install, and connect to mongodb in node.js on my remote server (ubuntu)? 我可以在node.js命令行窗口中逐行编写人偶脚本吗? - Can I script puppeteer line by line in the node.js command line window? 如何从另一个 Node.js 脚本中运行 Node.js 脚本 - How do I run a Node.js script from within another Node.js script 是否可以在 Node.JS 命令行程序中同时使用 CommonJS 和 ES6 模块? - Is it possible to use both CommonJS and ES6 module in a Node.JS command-line program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM