浏览化node.js http服务器 [英] Browserify the node.js http server

查看:163
本文介绍了浏览化node.js http服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们创建了一个简单的js文件,旨在找出http.createServer是否可以在客户端浏览器上运行:

We created a simple js file, intending to find out if http.createServer works on client browser or not:

var http = require("http")
var server = http.createServer()
server.listen(9024, function () {
    console.log("demo server listening on port 9024")
})

并在浏览器化后嵌入到html中。

and embedded it into a html after browserify.

显示chrome中的html,不幸的是,它总是在http.createServer()的第2行失败:

Display the html in chrome, unfortunately, it always fails on line 2 on http.createServer():

"Uncaught Type Error: undefined is not a function"

我们也玩过serve-browserify有点没有成功。

We also played around with "serve-browserify" a bit without success.

我们在chrome和firefox以及Linux和Windows上尝试过同样的事情。都失败了。
通过网络搜索,有很多例子可以将浏览器化为浏览器。
它们似乎都是对browserify的简单调用。但是我们似乎无法获得同样好的结果。
我们非常感谢您的帮助。

We have attempted the same thing on both chrome and firefox, and on Linux and Windows. All failed. Searching through the web, there are quite a few examples for browserify http into the browser. They all appear to be simple invocation of browserify. However we don't seem to be able to get the same good result. Your help will be greately appreciated.

推荐答案

您无法在浏览器中使用Node.js模块。所有Browserify都捆绑了CommonJS模块,它不允许您在浏览器中运行服务器端代码。

You can't use Node.js modules in the browser. All Browserify does is bundling CommonJS modules, it does not allow you to run server side code in the browser.

这篇关于浏览化node.js http服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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