node.js 使用 bat 文件运行节点服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20220477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:21:28  来源:igfitidea点击:

Run node server with a bat file

node.jsbatch-filenpm

提问by grimaldodev

I need to create a bat file to start node server, actually we do it manually but some people need extra help. Let me explain the process:

我需要创建一个 bat 文件来启动节点服务器,实际上我们是手动完成的,但有些人需要额外的帮助。让我解释一下这个过程:

  1. Open CMD
  2. Go to the path: cd C://user/folder/server/
  3. Run the server: npm start
  1. 打开命令
  2. 进入路径: cd C://user/folder/server/
  3. 运行服务器: npm start

It is very simple but I would really like to automate the process to make it faster.

这很简单,但我真的很想自动化这个过程以使其更快。

回答by Reza Ebrahimi

You can start node.jsserver as following .batscript file by click on it:

您可以通过单击node.js以下.bat脚本文件来启动服务器:

@echo off
echo.

set NodePackagesPath=E:\Projects\OpenShift\Materials\Node.jsPackageManager // This is my path, you can edit them

set Path=%NodePackagesPath%\node_modules\.bin;%PATH%
set Path=%NodePackagesPath%;%PATH%

set NODE_PATH=%NodePackagesPath%\node_modules;%NODE_PATH%
set NODE_ENV=production

echo Environment variables are successfully added.
echo. 
echo. 
echo. 

node server.js

回答by ddmh

Know this post is old, but just throwing it out there:I just made a super simple .bat-file to start it:

知道这篇文章很旧,但只是把它扔在那里:我刚刚制作了一个超级简单的 .bat 文件来启动它:

cd C:\nodejs

"C:\MongoDB\bin\mongod.exe" --dbpath C:\MongoDB\bin\data\db

node server.js

回答by MarkNg

First you must add node.js install folder to path.
Next make a bat file named start.bat
Then, in the file, write

首先,您必须将 node.js 安装文件夹添加到路径。
接下来制作一个名为start.bat的bat文件,
然后在文件中写入

C:/user/folder/server/
npm start

C:/user/folder/server/
npm start

Last click start.bat

最后点击start.bat

回答by Anonymous

Is it just me or is the answer already in plain sight? The following has worked for me perfectly (with OP's path):

是我一个人还是答案已经一目了然?以下对我来说非常有效(使用 OP 的路径):

cd "C:/user/folder/server"
node index.js

回答by Jordi Piqueras

This not really works: "START /WAIT bitsadmin.exe /transfer "Downloading" http://nodejs.org/dist/v0.8.11/%NODE_EXEC%C:\node-v0.8.11-x86.msi"

这不是真的有效:“START /WAIT bitsadmin.exe /transfer “Downloading” http://nodejs.org/dist/v0.8.11/%NODE_EXEC%C:\node-v0.8.11-x86.msi”

I don't know why, but the rest should work:

我不知道为什么,但其余的应该工作:

@echo off

NET SESSION >nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
    echo This setup needs admin permissions. Please run this file as admin.
    pause
    exit
)

set NODE_VER=null
set NODE_EXEC=node-v0.8.11-x86.msi
set SETUP_DIR=%CD%
node -v >tmp.txt
set /p NODE_VER=<tmp.txt
del tmp.txt
IF %NODE_VER% NEQ null (
    echo INSTALLING node ...
    mkdir tmp
    IF NOT EXIST tmp/%NODE_EXEC% (
        echo Node setup file does not exist. Downloading ...
        cd ../bin
        START /WAIT bitsadmin.exe /transfer "Downloading" http://nodejs.org/dist/v0.8.11/%NODE_EXEC% C:\node-v0.8.11-x86.msi
        rem START /WAIT wget http://nodejs.org/dist/v0.8.11/%NODE_EXEC%
        move %NODE_EXEC% %SETUP_DIR%/tmp
    )
    cd %SETUP_DIR%/tmp
    START /WAIT %NODE_EXEC%
    cd %SETUP_DIR%
) ELSE (
    echo Node is already installed. Proceeding ...
)

回答by John Smit Conor

Alternative method, will be to install Bash environment for Windows and create file with name start.sh

替代方法,将是为 Windows 安装 Bash 环境并创建具有名称的文件 start.sh

#!/usr/bin/env bash

npm start

Or

或者

#!/usr/bin/env bash

node <yourfilename>.js

回答by Fisheiyy

If you are trying to start a discord bot of some sorts then try this :

如果您正在尝试启动某种不和谐机器人,请尝试以下操作:

// this is my path so if your is different change it and this needs to stay open for the bot to run
cd C:\salmon\salmon
node bot.js