SORU
24 HAZİRAN 2012, Pazar


Nasıl node.js cross-origin resource sharing (CORS) express.js çerçevesinde etkinleştirmek için

Çapraz etki alanı hala statik dosyaları sağlarken betik, kamuya açık bir dizinden destek olacak node.js bir web sunucusu oluşturmak için çalışıyorum. Bu express.js ve gerçekten etki alanları arası komut dosyası (Access-Control-Allow-Origin: *) nasıl izin emin değilim kullanıyorum.

Yararlı olmak istemezdim this post, gördüm.

var express = require('express')
  , app = express.createServer();

app.get('/', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    next();
});

app.configure(function () {
    app.use(express.methodOverride());
    app.use(express.bodyParser());
    app.use(app.router);
});

app.configure('development', function () {

    app.use(express.static(__dirname   '/public'));
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function () {


    var oneYear = 31557600000;
    //    app.use(express.static(__dirname   '/public', { maxAge: oneYear }));
    app.use(express.static(__dirname   '/public'));
    app.use(express.errorHandler());
});

app.listen(8888);
console.log('express running at http://localhost:%d', 8888);

CEVAP
25 HAZİRAN 2012, PAZARTESİ


the example from enable-cors.org Check out:

Senin ExpressJS içinde uygulaması node.js sizin yolları ile şunları yapın:

app.all('/', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  next();
 });

app.get('/', function(req, res, next) {
  // Handle the get for this route
});

app.post('/', function(req, res, next) {
 // Handle the post for this route
});

İlk Çağrı (app.all) uygulama (veya KÖŞELERİ etkin olmasını istediğiniz en az olanlar) diğer tüm yolları önce yapılmalıdır.

[Düzenle]

Eğer başlıkları statik dosyalar için de göstermek için, (9 ** aramadan önce emin olun: . bu deneyin isterseniz

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  next();
});

Kodunuzu bu test, ve public dizinden varlıkların başlıkları var:

var express = require('express')
  , app = express.createServer();

app.configure(function () {
    app.use(express.methodOverride());
    app.use(express.bodyParser());
    app.use(function(req, res, next) {
      res.header("Access-Control-Allow-Origin", "*");
      res.header("Access-Control-Allow-Headers", "X-Requested-With");
      next();
    });
    app.use(app.router);
});

app.configure('development', function () {
    app.use(express.static(__dirname   '/public'));
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function () {
    app.use(express.static(__dirname   '/public'));
    app.use(express.errorHandler());
});

app.listen(8888);
console.log('express running at http://localhost:%d', 8888);

Tabii, şöyle bir şey yapabilirsin çok fonksiyonu bir modül içine paketi olabilir

// cors.js

module.exports = function() {
  return function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    next();
  };
}

// server.js

cors = require('./cors');
app.use(cors());

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • jpmkm1

    jpmkm1

    4 NİSAN 2008
  • PamtheBlamofficial

    PamtheBlamof

    31 Aralık 2010
  • WOSU Public Media

    WOSU Public

    23 AĞUSTOS 2007