diff --git a/controllers/search.js b/controllers/search.js index d41c378208..bf1bd01ef0 100644 --- a/controllers/search.js +++ b/controllers/search.js @@ -1,5 +1,9 @@ exports.index = function (req, res, next) { var q = req.query.q; q = encodeURIComponent(q); - res.redirect('https://www.google.com.hk/#hl=zh-CN&q=site:cnodejs.org+' + q); + if(req.query.notGG){ + res.redirect('https://cn.bing.com/search?q=site:cnodejs.org+' + q) + }else{ + res.redirect('https://www.google.com.hk/#hl=zh-CN&q=site:cnodejs.org+' + q); + } }; diff --git a/test/controllers/search.test.js b/test/controllers/search.test.js index dcfdf22e46..2836c59540 100644 --- a/test/controllers/search.test.js +++ b/test/controllers/search.test.js @@ -10,4 +10,12 @@ describe('test/controllers/search.test.js', function () { done(err); }); }); + it('should redirect to bing search when google is not reacheable', function (done) { + request.get('/search').query({q: 'node 中文',notGG:'1'}) + .expect(302) + .end(function (err, res) { + res.headers['location'].should.equal('https://cn.bing.com/search?q=site:cnodejs.org+node%20%E4%B8%AD%E6%96%87'); + done(err); + }); + }); }); diff --git a/views/layout.html b/views/layout.html index 7dfbcef1d1..353c1aca82 100644 --- a/views/layout.html +++ b/views/layout.html @@ -77,6 +77,7 @@ <form id='search_form' class='navbar-search' action="/search"> <input type='text' id='q' name='q' class='search-query span3' value=''/> + <input type='hidden' id='notGG' name='notGG' value='' /> </form> <ul class='nav pull-right'> <li><a href='/'>首页</a></li> @@ -146,8 +147,33 @@ ga('create', '<%-config.google_tracker_id%>', 'auto'); ga('send', 'pageview'); </script> + <% } %> +<script> + function checkSiteReachable(url,callback) { + var timer = setTimeout(function(){ + callback(false); + },2000) + + var img = document.createElement("img"); + img.onload = function() { + clearTimeout(timer); + callback(true); + } + img.onerror = function() { + clearTimeout(timer); + callback(false); + } + img.src = url+"/favicon.ico"; + } + checkSiteReachable('https://www.google.com',function(reachable){ + if(!reachable){ + document.querySelector('#notGG').value = '1'; + } + }) +</script> + <% if (config.cnzz_tracker_id) { %> <div style="display:none;"> <script src="//s95.cnzz.com/z_stat.php?id=<%- config.cnzz_tracker_id %>&web_id=<%- config.cnzz_tracker_id %>" language="JavaScript"></script>