简体   繁体   中英

how to run a simple node.js file using batch file

I have one simple "Hello World" program in Node.js. I want to run this using batch file.

 var http=require("http"); http.createServer(function(request,response){ response.writeHead(200,{'Content-Type':'text/plain'}); response.end('Hello World!..\\n'); }).listen(8080); console.log('Server running at http://127.0.0.1:8080/'); 
I have used this batch file named 'new.bat'.

 if not "%minimized%"=="" goto :minimized set minimized=true @echo off start /min cmd /c "C:\\Users\\USER2\\Desktop\\webchat\\server.js" goto :EOF :minimized 

please help me.

You can try this code to create a batch file:

if not "%minimized%"=="" goto :minimized
set minimized=true
@echo off

cd "C:\app path"

start /min cmd /C "nodemon server.js"
goto :EOF
:minimized

to run node.js in background.

Instead of running your app like you are used to like node server.js , you can use nodemon: nodemon server.js

The most simple way you can imagine for a node package:

npm install -g nodemon

server.js

var http=require("http");

http.createServer(function(request,response){
    response.writeHead(200,{'Content-Type':'text/plain'});
    response.end('Hello World!..\n');
}).listen(8080);
console.log('Server running at http://127.0.0.1:8080/');

new.bat 在此输入图像描述

if not "%minimized%"=="" goto :minimized
set minimized=true
@echo off
cd "C:\Users\USER2\Desktop\webchat"

start /min cmd /C "nodemon server.js"
goto :EOF
:minimized

Run the application in cmd: 在cmd中运行应用程序 在cmd中运行应用程序后,node.js命令提示符将如下所示..应用程序运行成功 现在,您可以通过设置localhost和端口来查看浏览器中的输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM