2016-03-06 80 views
-1

我有3個文件,一個簡單的.html的node.js:模塊在不工作

<!DOCTYPE html> 
<html> 
<head> 
    <title>meh server</title> 
</head> 

<body> 
    <script src = "index.js"></script> 
</body> 
</html> 

的 'server.js':

module.exports.proto = function() { 
     alert("Working function"); 
    } 
}; 

和 'index.js':

var server = require('./server'); 

server.proto(); 

全部在同一個文件夾中。我在我的PC上安裝了node.js,並在3個文件的文件夾中的Windows cmd「npm install nodejs」上鍵入。我無法收到來自server.js的警報,我不知道爲什麼。

回答

0

首先,你的server.js文件中有語法錯誤。

module.exports.proto = function() { 
    alert("Working function"); 
}; 

其次,節點中沒有'alert'函數。你可以寫CONSOLE.LOG代替

module.exports.proto = function() { 
     console.log("Working function"); 
    }; 

然後從命令提示符下運行index.js

node index.js 

你可以看到消息在命令提示符下「工作的功能」。

在瀏覽器中打開您的html文件不會以相同的方式工作。你真的需要了解節點:)

編輯: index.js

var server = require('./server'); 

var http = require('http'); 
fs = require('fs'); 

http.createServer(function(req, res) { 
    server.proto(); 
    fs.readFile('index.html', 'binary', function(err, file) { 
     if(err) { 
     res.writeHead(500, {"Content-Type": "text/plain"}); 
     res.write(err + "\n"); 
     res.end(); 
     return; 
     } 

     res.writeHead(200, {"Content-Type": "text/html"}); 
     res.write(file, "binary"); 
     res.end(); 
     }); 
}).listen(3000, function() { 
    console.log("Server started"); 
}); 

現在,如果您從命令提示符index.js去爲localhost:3000在你的瀏覽器,可以看到它按照你真正想要的方式工作。

0

哦!alert沒有任何東西被node.js識別,它是瀏覽器的一部分。

而不是使用alert你應該記錄你的消息,這也似乎你不是不可複製完整的代碼或者這真是一個語法錯誤,因此要修復它:

module.exports.proto = function() { 
    console.log("Working function"); 
} 

現在你可以使用您的IDE運行index.js或轉到任何shell並拍攝node index,它將運行節點代碼。

其次如果你希望你的node.js服務器在瀏覽器Follow This Tutorial

+0

它爲你工作? –

+0

是的,它的確如此。 thxxx –

+0

很好,[node-cheat](https://github.com/zishon89us/node-cheat) –

0

好發送或打開html文件,安裝Node.js的是一個開始,但好像你是無所適從的node.js是。節點是運行時。是的,它建立在V8之上(Google Chrome中的同一個JavaScript VM),但沒有DOM,沒有瀏覽器API,也絕對不需要瀏覽器。因此,alert不存在於Node中。我建議熟悉API docs

正如之前所回答的那樣,通過執行node二進制文件並將文件作爲參數(有一個repl和其他選項)傳遞,就像運行python或ruby程序一樣運行節點程序。在你的情況下:

node index.js 
1

首先,Node.js是一個運行時,沒有DOM。所以'alert'沒有在節點中定義。

其次,你需要通過執行節點的二進制&傳遞文件名作爲命令行參數來運行節點程序,如

node your_file_name.js 

對於在瀏覽器中獲取響應無論是在命令行&你需要做的以下:

命令行:

文件:server.js

module.exports.proto = function() { 
    console.log("Working function"); 
} 

文件:index.js

var server = require("./server"); 
server.proto(); 

現在在命令行中運行以下命令:

node index.js 

,你會看到在命令行你所需的輸出。

瀏覽器:

文件:server.js

module.exports.proto = function() { 
    return "Working function"; 
} 

文件:index.js

var server = require("./server"); 

var httpServer = require("http"); 
httpServer.createServer(function (request, response) { 
    response.writeHead(200, {"Content-Type": "text/html"}); 
    response.write(server.proto()); 
    response.end(); 
}). 
listen(3000, function() { 
    console.log("server listening on 3000"); 
}); 

現在在命令行中運行以下命令:

node index.js 

你將在公司中看到以下內容mmand行:

server listening on 3000 

現在就去瀏覽器&打以下:

http://localhost:3000/ 

您將看到您在瀏覽器所需的輸出。

***欲瞭解更多信息,我會建議查看'HTTP'節點的API。 https://nodejs.org/api/http.html

謝謝。希望它有幫助...