AJAX请求到我的节点服务器 - 没有通过

时间:2017-01-01 04:50:16

标签: ajax node.js express https

我目前正在将流量重定向到我的节点服务器上的https:

var express = require('express');
var path = require('path');

var app = express();
var port = process.env.PORT || 8080;
app.set('port', port);
app.use(express.static(path.join(__dirname, './src/public')));

app.all('*', function(req, res, next) {
  if(req.headers["x-forwarded-proto"] === "https"){
    // OK, continue
    return next();
  };
  res.redirect('https://'+req.hostname+req.url);
})

app.get('*', function( req, res ) {
  res.sendFile(path.join(__dirname, './src/public/index.html'));
} );

app.use(require('./server/routes/index'));

app.listen(process.env.PORT || 8080, function (){
    console.log('Express started on port ' + app.get('port') + '; press Ctrl-C to terminate.');
  });

module.exports = app;

这样做,我的AJAX呼叫无法通过:net::ERR_CONNECTION_REFUSED。当然,当我删除上面的重定向时,我的AJAX调用才能完成。

我设法在有这个问题的人身上找到另一篇关于SO的文章,但建议是将Access-Control-Allow-Origin设置为接受任何传入的AJAX请求,这似乎是一个安全问题。

如果我想继续使用https重定向,是否有办法允许通过AJAX调用,或者我是否需要关闭重定向?

2 个答案:

答案 0 :(得分:0)

使用 req.protocal req.get('host')获取协议和主机网址

 app.all('*', function(req, res, next) {
      if(req.headers["x-forwarded-proto"] === "https"){
        // OK, continue
        return next();
      };
      res.redirect(req.protocol + '://' + req.get('host') + req.originalUrl);
    })

答案 1 :(得分:0)

接下来接受另一个回调,如

$( ".rotate-slider li" ).hover(function() { clearInterval(rotateInterval); }, function() { rotateInterval = window.setInterval(function(){ if(!rotateSlider.slidesContainer.hasClass('animate')){ rotateSlider.slidesContainer.addClass('animate') } currentRotation = currentRotation - rotateSlider.slideAngle; rotateSlider.slidesContainer.css('transform', 'translateX(-50%) rotate('+currentRotation+'deg)'); }, 4000); });

让你有一个索引页面然后它应该

app.all(path, callback [, callback ...])
相关问题