browserify错误:http.createServer不是函数 [英] browserify Error : http.createServer is not a function

查看:148
本文介绍了browserify错误:http.createServer不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试浏览此节点js脚本:

I tried to browserify this node js script :

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

当我从html文件运行bundle.js时出现此错误:

and when I run bundle.js from an html file I get this error:

http.createServer is not a function 

似乎browserify不支持httpserver。如何解决此问题?

it seems that browserify does not support httpserver. How can I resolve this problem?

推荐答案

您无法在Web浏览器中运行Web服务器。浏览器中确实没有任何东西可以像Node的 http 模块一样。此外,在浏览器中运行PhantomJS没有意义,因为PhantomJS 是一个Web浏览器。

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. Also it doesn't make sense to run PhantomJS in a browser, because PhantomJS is a web browser.

您需要的行为是什么?试图完成?

What is the desired behavior you are trying to accomplish?

更新:

看起来像你试图在浏览器中运行用于Node.js的代码。

It seems like you are trying to run code intended for Node.js inside a browser instead.

浏览器中的JavaScript引擎比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).

Browserify会< a href =https://github.com/substack/node-browserify#compatibility =nofollow>包含一些垫片,它们会将小型JS库放入您的浏览器代码中匹配Node.js的API,允许某些 Node.js特定的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.

在你的情况下,你是需要 Phantom ,其中se ems反过来要求 http 。根据Browserify文档,它将看到 require('http')并包含 http模块的垫片(因为浏览器不提供自己的 http 模块。)

In your case, you are requiring Phantom, which seems to in turn require 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).

Phantom 模块然后尝试调用 http.createServer()但是到该http shim的文档:

The Phantom module then tries to call http.createServer() but accoring to the documentation for that http shim:


这个模块实现了http.request,http.get和大部分http.ClientRequest和http。除了http.METHODS和http.STATUS_CODES之外的IncomingMessage。

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.createServer()。这也是有道理的,因为浏览器永远不会让你自己打开一个http服务器,否则导航到某人的网站可能会导致你的浏览器开始向外界提供内容,这显然没有意义。

so http.createServer() is not supported by the shim. 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.

在你的评论中:


我希望我的节点js脚本可以从另一个JS代码

"i want that my node js script can be executed from another JS code"

你没有指定正在运行的其他JS代码。如果那个JS代码已经在运行节点,那么你根本不需要Browserify。如果您正在尝试使用Web浏览器启动实际的Node.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. 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.

Browserify允许你做的是获取最初用于Node.js的代码,而不是在浏览器中运行它,但是在运行时它在浏览器中执行,不在Node.js中,所以你只能使用在浏览器JS运行时的约束下工作的JS代码。

What Browserify lets you do is take code originally intended for Node.js, and run it in a browser instead, but a t 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.

如果你试图在Node中执行你的代码。 js,然后你需要通过从命令行启动Node.js可执行文件或通过让另一个程序为你启动进程来做到这一点,但这不能在Web浏览器中完成。如果您尝试让用户导航到您的网站,然后让这些代码在他们的计算机上在浏览器中而不是在Node.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. 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.

这篇关于browserify错误:http.createServer不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆