2017-04-27 22 views
2

我在Node.js中有一个简单的“Hello World”程序。我想用批处理文件运行这个。如何使用批处理文件运行简单的node.js文件

var http=require("http"); 
 

 
http.createServer(function(request,response){ 
 
\t response.writeHead(200,{'Content-Type':'text/plain'}); 
 
\t 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 
start /min cmd /c "C:\Users\USER2\Desktop\webchat\server.js" 
goto :EOF 
:minimized 

此批处理文件,请帮我

回答

4

你可以试试这个代码来创建一个批处理文件:

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

cd "C:\app path" 

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

在后台运行node.js。

而是像你以前喜欢 node server.js运行你的应用程序,你可以使用nodemon:nodemon server.js

最简单的方法,你可以想像一个节点包:

npm install -g nodemon 
+0

喔好,我将安装并检查..什么是nodemon? –

+0

如果您在node.js.中做了任何更改,您不必再次刷新.nodemon将自动刷新文件 –

+0

欢迎@nagaraj我认为您知道如何创建批处理文件 –

3

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 enter image description here

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 

运行在cmd中的应用: run the application in cmd after running the application in cmd the node.js command prompt will look like this.. the application running successfully now you can see the output in browser by setting localhost and the port