簡體   English   中英

如何將Google Chrome用作node.js服務器?

[英]How can I use Google Chrome as a node.js server?

我將節點js hello world示例復制/粘貼到html文件中

<html>
<head>
<script>
var http = require("http");

http.createServer(function (request, response) {

   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});

   // Send the response body as "Hello World"
   response.end('Hello World\n');
}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');

</script>

</head>
</html>

我正在使用名為OK 200的Chrome應用! 用於在http://127.0.0.1:8887/上提供此頁面的服務器理想的解決方案適用於Google Chromebook。

當我運行它時,它會給出錯誤:

未捕獲的ReferenceError:未定義require(...)

這意味着節點中獨有的幾個功能將不會嵌入Chrome中。 是否有擴展程序,Chrome應用程序或本機客戶端應用程序可以使這些功能可用並仍然利用已嵌入Chrome的v8引擎?

你可以用像電子這樣的東西。

它基本上為您提供了node.js和chromium,使您能夠構建跨平台的桌面應用程序。

我不確定你的目標嗎? 您是否嘗試從客戶端Web瀏覽器運行服務器? 我不確定這是否可能有很多原因(例如安全性)......我認為你需要閱讀這個

如果您只是想在localhost(127.0.0.1)上運行節點服務器,則需要在計算機Node.js上安裝nodejs,然后需要將nodejs示例保存在javascript文件中(名為“myServer.js”)例如)並使用以下命令在CLI中運行節點:

node path/to/myServer.js

關於:

Uncaught ReferenceError: require is not defined

你應該看到#19059580

最誠摯的問候,特里斯坦

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM