如何将Google Chrome用作node.js服务器?

我复制/粘贴节点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/上提供此页面。理想的解决scheme适用于Google Chromebook。

当我运行这个错误:

Uncaught ReferenceError:require is not defined(…)

所以这意味着一些独特的节点function将不会embedded到Chrome中。 是否有一个扩展,铬应用程序,或本地客户端应用程序,将使这些function可用,仍然利用已经embedded在Chrome中的V8引擎?

我不确定你的目标? 你是否试图从客户端Web浏览器运行服务器? 我不确定这是可能的,因为很多原因(例如安全)…我想你需要阅读这个 !

如果你只是想在你的本地主机(127.0.0.1)上运行一个节点服务器,你需要在你的计算机Node.js上安装nodejs,那么你需要将你的nodejs例子保存在JavaScript文件(名为“myServer.js”例如)并在CLI中使用以下命令运行节点:

 node path/to/myServer.js 

而关于:

 Uncaught ReferenceError: require is not defined 

你应该看#19059580

最好的问候,特里斯坦

你可以使用电子等东西。

它基本上为您提供node.js和chromium,并使您能够构build跨平台的桌面应用程序。