2017-04-27 24 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