简体   繁体   English

browserify错误:http.createServer不是函数

[英]browserify Error : http.createServer is not a function

I tried to browserify this node js script : 我试图浏览器将此节点js脚本:

var phantom = require('phantom')
phantom.create(function(ph) {
ph.createPage(function(page) {
    page.open("editor.html", function(status) {
        console.log("opened diagram? ", status);
        page.evaluate(function() {
            return document.getElementById("GraphImage").src;
        }, function(result) {
            //console.log(result);
            ph.exit();
        });
    });
});
});

So I used this command: 因此,我使用了以下命令:

browserify myscript.js > bundle.js

and when I run bundle.js from an html file I get this error: 当我从html文件运行bundle.js时,出现以下错误:

http.createServer is not a function 

it seems that browserify does not support httpserver. 似乎browserify不支持httpserver。 How can I resolve this problem? 我该如何解决这个问题?

You can't run a web server from inside a web browser. 您无法从网络浏览器内部运行网络服务器。 There really isn't anything in the browser that could act like Node's http module. 浏览器中确实没有任何东西可以像Node的http模块那样工作。 Also it doesn't make sense to run PhantomJS in a browser, because PhantomJS is a web browser. 同样,在浏览器中运行PhantomJS也没有意义,因为PhantomJS Web浏览器。

What is the desired behavior you are trying to accomplish? 您要完成的期望行为是什么?


Update: 更新:

It seems like you are trying to run code intended for Node.js inside a browser instead. 似乎您正在尝试在浏览器中运行旨在用于Node.js的代码。

The JavaScript engine inside the browser is much more restrictive than in Node.js, for example you can't access the file system from inside the browser for security reasons (or else you could read the hard drive of anyone who visited your web page). 浏览器中的JavaScript引擎比Node.js中的限制要严格得多,例如,出于安全原因,您不能从浏览器内部访问文件系统(否则,您可以阅读访问您网页的任何人的硬盘驱动器) 。

Browserify does include some "shims" that will put small JS libraries into your code that work in the browser and match the API of Node.js, allowing some Node.js specific JS code to execute in the browser. Browserify确实包含一些“垫片”这些垫片会将小的JS库放入可以在浏览器中工作的代码中,并与Node.js的API匹配,从而允许某些特定于Node.js的JS代码在浏览器中执行。

In your case, you are requiring Phantom , which seems to in turn require http . 在您的情况下,您需要Phantom ,而Phantom似乎又需要http Accoring to the Browserify documentation, it will see require('http') and include a shim for the http module (because browser's don't provide an http module of their own). 根据Browserify文档,它将看到require('http')并包含http模块的填充程序 (因为浏览器不提供自己的http模块)。

The Phantom module then tries to call http.createServer() but accoring to the documentation for that http shim: 然后, Phantom模块尝试调用http.createServer()但根据该http填充程序的文档:

This module implements http.request, http.get, and most of http.ClientRequest and http.IncomingMessage in addition to http.METHODS and http.STATUS_CODES. 除了http.METHODS和http.STATUS_CODES之外,此模块还实现http.request,http.get和大多数http.ClientRequest和http.IncomingMessage。

so http.createServer() is not supported by the shim. 因此,垫片不支持http.createServer() This also makes sense because a browser would never let you open an http server inside of itself, or else navigation to someone's web site could cause your browser to start serving content to the outside world, which obviously doesn't make sense. 这也是有道理的,因为浏览器永远不会让您在其内部打开http服务器,否则导航到某人的网站可能会导致您的浏览器开始向外部提供内容,这显然没有意义。

In your comment: 在您的评论中:

"i want that my node js script can be executed from another JS code" “我希望可以从另一个JS代码执行我的节点js脚本”

You don't specify what "other JS code" is running in. If that JS code is already running in Node, then you don't need Browserify at all. 您无需指定正在运行的“其他JS代码”。如果该JS代码已在Node中运行,则根本不需要Browserify。 If you are trying to have a web browser start up an actual Node.js process, that isn't going to happen, again for obvious security reasons, because browsing to a web page shouldn't have the ability to run any executable on your system. 如果您试图让网络浏览器启动实际的Node.js进程,出于明显的安全原因,这不会发生,因为浏览网页不应该能够在您的网页上运行任何可执行文件系统。

What Browserify lets you do is take code originally intended for Node.js, and run it in a browser instead, but at runtime it is executing in the browser, not in Node.js, so you can only use JS code that works within the constraints of the browser's JS runtime. Browserify允许您执行的操作是获取最初用于Node.js的代码,然后在浏览器中运行,但是在运行时,它是在浏览器中执行的,而不是在Node.js中执行的,因此您只能使用在浏览器的JS运行时的约束。

If you are trying to execute your code in Node.js, then you need to do that by having something start the Node.js executable, either from the command line or by having another program start the process for you, but that can't be done from within a web browser. 如果要尝试在Node.js中执行代码,则需要通过从命令行或其他程序为您启动进程来启动Node.js可执行文件的方式来完成,但这不能可从网络浏览器中完成。 If you are trying to have users navigate to your web site and then have this code run on their machines in a browser and not in Node.js, then you need to only use modules that work in the browser. 如果您试图让用户导航到您的网站,然后让该代码在他们的计算机上通过浏览器而不是在Node.js中运行,那么您只需要使用在浏览器中有效的模块即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM