在Browserify中完全使用Node的http模块

时间:2016-06-30 09:42:41

标签: javascript node.js browserify

我想在普通浏览器中运行以下Node.js代码,例如Chrome:

testInstrumentationRunner "com.company.myapp.TestRunner"

当代码在浏览器上正确运行时,会传递两个断言而没有任何错误。

它需要与浏览器兼容的var http = require('http') var assert = require('assert') http.createServer((req, res) => { // Send hello greetings for all clients. res.writeHead(200, { 'Content-Type': 'text/html' }) res.end('<h1>Hello, world!</h1>') }).listen(3000, () => { // Get the response myself. http.request({ hostname: 'localhost', port: 3000, path: '/', method: 'GET' }, res => { assert.equals(res.statusCode, 200) // Get the chunked content of the response. res.setEncoding('utf8') var content = '' res.on('data', (chunk) => { content += chunk }) res.on('end', () => { // Finish to get the chunked. assert.equals(content, '<h1>Hello, world!</h1>') }) }) }) 函数才能在浏览器中运行Node代码。 我找到了一个合适的库Browserify,但是有not fully-implemented http module的Node.js。

很遗憾,我无法使用require(...)http.createServer(...)函数。

如何在浏览器上正确运行节点代码?

加成: 它并不要求http.listen(...) 托管新服务器,只是模拟。 http.createServer(...)http.request(...)相同。

我认为可以模拟使用模拟对象。

0 个答案:

没有答案