【问题标题】:How can I use Google Chrome as a node.js server?如何将 Google Chrome 用作 node.js 服务器?
【发布时间】:2017-03-26 20:10:25
【问题描述】:

我将一个 node 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 上运行。

当我运行它时,它会给出错误:

Uncaught ReferenceError: require is not defined(…)

这意味着 node 独有的一些功能不会嵌入到 Chrome 中。是否有扩展程序、chrome 应用程序或本机客户端应用程序可以使这些功能可用并仍然利用 Chrome 中已经嵌入的 v8 引擎?

【问题讨论】:

标签: javascript node.js google-chrome


【解决方案1】:

我不太确定你的目标?您是否尝试从客户端 Web 浏览器运行服务器?我不确定这是否可能出于多种原因(例如安全性)...我认为您需要阅读this

如果你只是想在你的 localhost (127.0.0.1) 上运行一个节点服务器,你需要在你的计算机上安装 nodejs Node.js,然后你需要将你的 nodejs 示例保存在 javascript 文件中(名为“myServer .js" 例如)并在 CLI 中使用以下命令运行节点:

node path/to/myServer.js

关于:

Uncaught ReferenceError: require is not defined

你应该看到#19059580

最好的问候, 特里斯坦

【讨论】:

  • 我的目标是看看是否有办法让动态网络服务器在 Google Chromebook 上运行?
  • 看一下here
【解决方案2】:

你可以使用electron之类的东西。

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

【讨论】:

    猜你喜欢
    • 2016-11-25
    • 2012-07-22
    • 2013-05-23
    • 2011-06-21
    • 2018-12-15
    • 2017-04-29
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    相关资源
    最近更新 更多