繁体   English   中英

通过 Node.js 路由 http 请求

[英]Routing http requests through Node.js

我正在尝试使用 Node.js 进行黄瓜测试设置,该设置可以使用 iframe 测试任何网站。 由于跨脚本安全限制,通常 iframe 是不可行的。 但是,如果有可能(我确定是。我相信您会提出解决方案)在请求特定 url 名称时通过请求的 url 获取作为测试目标的网站,以便 iframe将加载测试目标的副本。 基本上只是一个标准的 node.js 服务器,它根据类似于地址请求路由器的 req.url 获取特定页面。

这是我公然尝试这样做的。 通过获取测试页。 网址有效。 但是我在从 http 服务器切换到连接对象时遇到了问题。 有没有办法“馈送”与 http 服务器响应的连接?

附注。 我还创建了一个带有两个 node.js 服务器的解决方案。 节点 1 获取测试目标并将其与黄瓜测试页面混合。 托管黄瓜测试的节点 2。 此解决方案有效。 但它会在发生 javascript 命名冲突的网站上产生问题。 这就是为什么通过封装解决这个问题的 iframe 解决方案更有吸引力。

var http  = require('http');
var connect    = require('connect');
var port  = process.env.PORT || 8788;

var server = http.createServer(function(req, webres)
{
    var url = req.url;
    console.log(url);

    if(url == '/myWebsiteToBeTestedWithCucumberJS')
    {
        // Load the web site to be tested "myWebsiteToBeTestedWithCucumberJS"
            // And update the references
            // Finaly write the page with the webres
            // The page will appear to be hosted locally

        console.log('Loading myWebsiteToBeTestedWithCucumberJS');
        webres.writeHead(200, {'content-type': 'text/html, level=1'});
        var options =
        {  
                   host: 'www.myWebsiteToBeTestedWithCucumberJS.com,   
                   port: 80,   
                   path: '/'
        };

        var page = '';
        var req = http.get(options, function(res)
        {
            console.log("Got response: " + res.statusCode);   
            res.on('data', function(chunk)
            {
                page = page + chunk;
            });   
            res.on('end', function()
            {
                    // Change relative paths to absolute (actual web location where images, javascript and stylesheets is placed)
                    page = page.replace(/ href="\/\//g       , ' href="/');
                    page = page.replace(/ src="\//g          , ' src="www.myWebsiteToBeTestedWithCucumberJS.com');
                    page = page.replace(/ data-src="\//g     , ' data-src="www.myWebsiteToBeTestedWithCucumberJS.com');
                    page = page.replace(/ href="\//g         , ' href="www.myWebsiteToBeTestedWithCucumberJS.com');

                    webres.write(page);
                    webres.end('');
            });
        });
    }
    else
    {
        // Load any file from localhost:8788
            // This is where the cucumber.js project files are hosted
        var dirserver     = connect.createServer();
        var browserify = require('browserify');
        var cukeBundle = browserify({
          mount: '/cucumber.js',
          require: ['cucumber-html', './lib/cucumber', 'gherkin/lib/gherkin/lexer/en'],
          ignore: ['./cucumber/cli', 'connect']
        });
        dirserver.use(connect.static(__dirname));
        dirserver.use(cukeBundle);
        dirserver.listen(port);
    }
}).on('error', function(e)
{  
      console.log("Got error: " + e.message);   
});
server.listen(port);
console.log('Accepting connections on port ' + port + '...');

嗯,这毕竟不是那么困难。
作为 node.js 的新手,我必须意识到使用多个侦听器的可能性。
阅读nodejitsu的功能帮助我解决了这个问题。

下面的示例在指定 url 时加载 www.myWebsiteToBeTestedWithCucumberJS.com,如下所示: http://localhost:9788/myWebsiteToBeTestedWithCucumberJS ,其中所有其他请求都被处理为http://localhost:9788/myWebsiteToBeTestedWithCucumberJS网站请求。
希望这对其他 node.js 新黄瓜有意义。

var http  = require('http');

var connect    = require('connect');
var port  = process.env.PORT || 9788;

var server = http.createServer(function(req, webres)
{
    var url = req.url;
    console.log(url);
    if(url == '/myWebsiteToBeTestedWithCucumberJS')
    {
        loadMyWebsiteToBeTestedWithCucumberJS(req, webres);
    }
    else
    {
        loadLocal(req, webres, url);
    }
}).on('error', function(e)
{  
      console.log("Got error: " + e.message);   
});
server.listen(port);
console.log('Accepting connections on port ' + port + '...');

function loadMyWebsiteToBeTestedWithCucumberJS(req, webres)
{
    console.log('Loading myWebsiteToBeTestedWithCucumberJS');
    webres.writeHead(200, {'content-type': 'text/html, level=1'});
    var options =
    {  
               host: 'www.myWebsiteToBeTestedWithCucumberJS.com',   
               port: 80,   
               path: '/'
    };

    var page = '';
    var req = http.get(options, function(res)
    {
        console.log("Got response: " + res.statusCode);   
        res.on('data', function(chunk)
        {
            page = page + chunk;
        });   
        res.on('end', function()
        {
                page = page.replace(/ href="\/\//g       , ' href="/');
                page = page.replace(/ src="\//g          , ' src="http://www.myWebsiteToBeTestedWithCucumberJS.com/');
                page = page.replace(/ data-src="\//g     , ' data-src="http://www.myWebsiteToBeTestedWithCucumberJS.com/');
                page = page.replace(/ href="\//g         , ' href="http://www.myWebsiteToBeTestedWithCucumberJS.com/');

                webres.write(page);
                webres.end('');
        });
    });

}

function loadLocal(req, webres, path)
{
    console.log('Loading localhost');
    webres.writeHead(200, {'content-type': 'text/html, level=1'});
    var options =
    {  
               host: 'localhost',   
               port: 9787,   
               path: path
    };

    var page = '';
    var req = http.get(options, function(res)
    {
        console.log("Got response: " + res.statusCode);   
        res.on('data', function(chunk)
        {
            page = page + chunk;
        });   
        res.on('end', function()
        {
                webres.write(page);
                webres.end('');
        });
    });
}


// Cucumber site listening on port 9787
var dirserver     = connect.createServer();
var browserify = require('browserify');
var cukeBundle = browserify(
{
    mount: '/cucumber.js',
    require: ['cucumber-html', './lib/cucumber', 'gherkin/lib/gherkin/lexer/en'],
    ignore: ['./cucumber/cli', 'connect']
});
dirserver.use(connect.static(__dirname));
dirserver.use(cukeBundle);
dirserver.listen(9787);
var http = require('http'); 

// Create a server object 
http.createServer(function (req, res) { 

    // http header 
    res.writeHead(200, {'Content-Type': 'text/html'}); 

    var url = req.url; 

    if(url ==='/about') { 
        res.write(' Welcome to about us page'); 
        res.end(); 
    } 
    else if(url ==='/contact') { 
        res.write(' Welcome to contact us page'); 
        res.end(); 
    } 
    else { 
        res.write('Hello World!'); 
        res.end(); 
    } 
}).listen(3000, function() { 

    // The server object listens on port 3000 
    console.log("server start at port 3000"); 
}); 

暂无
暂无

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

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