【发布时间】: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 引擎?
【问题讨论】:
-
不在普通的 Chrome 浏览器中。标准浏览器不具备运行任何类型服务器的能力,更不用说访问 node.js 的其余功能了。您将需要简单的代码(单独的应用程序或插件)来添加所需的功能以使创建服务器成为可能。
标签: javascript node.js google-chrome