Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将Google Chrome用作node.js服务器?_Javascript_Node.js_Google Chrome - Fatal编程技术网

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

Javascript 如何将Google Chrome用作node.js服务器?,javascript,node.js,google-chrome,Javascript,Node.js,Google Chrome,我将一个节点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 re

我将一个节点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>

var http=require(“http”);
createServer(函数(请求、响应){
//发送HTTP头
//HTTP状态:200:正常
//内容类型:文本/纯文本
writeHead(200,{'Content-Type':'text/plain'});
//将响应正文作为“Hello World”发送
response.end('Hello World\n');
}).听(8081);
//控制台将打印消息
console.log('服务器在运行http://127.0.0.1:8081/');
我正在使用一个叫OK 200的Chrome应用程序!使此页在上可用的服务器 理想的解决方案是在谷歌Chromebook上工作

当我运行此命令时,会出现以下错误:

未捕获引用错误:未定义require(…)


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

你可以用类似的东西


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

我不太确定您的目标是什么?您正在尝试从客户端web浏览器运行服务器吗?由于许多原因,我不确定这是否可行(例如安全性)。。。我想你需要阅读

如果您只是尝试在本地主机(127.0.0.1)上运行节点服务器,则需要在计算机上安装nodejs,然后需要将nodejs示例保存在javascript文件(例如名为“myServer.js”)中,并使用以下命令在CLI中运行node:

node path/to/myServer.js
关于:

Uncaught ReferenceError: require is not defined
你应该看到

致以最良好的祝愿,
Tristan不在纯Chrome浏览器中。标准浏览器没有运行任何类型服务器的功能,更不用说访问node.js的其余功能了。您需要朴素的代码(单独的应用程序或插件),可以添加所需的功能,使创建服务器成为可能。你也可以构建自己的chromium版本。我的目标是看看是否有一种方法可以在Google Chromebook上运行动态web服务器?那么看看吧