2016-08-25 45 views
2

這東西一直在竊聽我好幾天了,我即將瘋狂......我無法set.header('訪問控制 - 允許起源')或任何其他這件事的頭。我需要這個來訪問一個遠程API。我已經下載了Chrome Cors擴展,並且在啓用時一切正常。當我禁用時,我得到了美麗的錯誤:沒有'訪問控制允許來源'標題出現在請求的資源。因此不允許訪問原產地'http://localhost:8080'。不能set.header在快遞

我已經嘗試了一些事情,甚至Cors npm模塊,仍然是一樣的。

這是我的代碼:

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

app.use('/dist', express.static(__dirname + '/dist')); 


app.use(function(req, res, next) { 
    res.header('Access-Control-Allow-Origin', '*'); 
    res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE'); 
    res.header('Access-Control-Allow-Headers', 'Content-Type'); 
    next(); 
}); 

var port = process.env.PORT || 8080; 


app.get('*', function(req, res, next) { 
    console.log(req.get('Access-Control-Allow-Origin')); 
    res.sendFile(__dirname + '/dist/index.html'); 
}); 


app.listen(port); 
console.log('listen at ' + port); 
+0

你試過'res.append(...'? –

+0

不,我沒有..但現在嘗試它,但仍然無法正常工作... – Daniel

+0

您正在使用哪個版本的'express'? –

回答

0

請檢查了這一點:

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

function setHeaders(res, path, stat){ 
res.append('Access-Control-Allow-Origin', '*'); 
    res.append('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE'); 
    res.append('Access-Control-Allow-Headers', 'Content-Type'); 
} 

app.use('/dist', express.static(__dirname + '/dist', {setHeaders: setHeaders})); 


/*app.use(function(req, res, next) { 
    res.header('Access-Control-Allow-Origin', '*'); 
    res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE'); 
    res.header('Access-Control-Allow-Headers', 'Content-Type'); 
    next(); 
});*/ 

var port = process.env.PORT || 8080; 


/*(app.get('*', function(req, res, next) { 
    console.log(req.get('Access-Control-Allow-Origin')); 
    res.sendFile(__dirname + '/dist/index.html'); 
});*/ 


app.listen(port); 
console.log('listen at ' + port); 

http://localhost:8080/dist/index.html你可以看到設置的所有頭前

+0

酷!所有的頭文件都在那裏,但是在對API進行獲取請求時,我仍然得到相同的錯誤... – Daniel

+0

@Daniel哪個API?和什麼錯誤? –

+0

API到namecheap。它在我使用鉻擴展啓用cors時起作用。 我正在使用axios.get() 而我得到這個錯誤:沒有'Access-Control-Allow-Origin'標題出現在請求的資源上。原因'http:// localhost:8080'因此不被允許訪問。 – Daniel

0

嘗試,這也:

app.use(function(req, res, next) { 

    res.setHeader('Access-Control-Allow-Origin', "*"); 
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE'); 
    res.setHeader('Access-Control-Allow-Headers', 'authorization, content-type'); 
    next(); 
}); 
0

Th是我的標題是如何在鉻擴展現場頭...尋找

GET /dist/index_bundle.js HTTP/1.1 
    Host: localhost:8080 
    Accept: text/css,*/*;q=0.1 
    Accept-Encoding: gzip, deflate, sdch, br 
    Accept-Language: nb-NO,nb;q=0.8,no;q=0.6,nn;q=0.4,en-US;q=0.2,en;q=0.2 
    Origin: http://localhost:8080 
    Referer: http://localhost:8080/ 
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 
    X-DevTools-Emulate-Network-Conditions-Client-Id: 54cfead4-1b26-46cd-8014-d16327ce17d7 

    HTTP/1.1 304 Not Modified 
    Accept: * 
    Accept-Ranges: bytes 
    Access-Control-Allow-Headers: * 
    Access-Control-Allow-Methods: * 
    Access-Control-Allow-Origin: * 
    Cache-Control: public, max-age=0 
    Connection: keep-alive 
    Date: Thu, 25 Aug 2016 12:03:29 GMT 
    ETag: W/"7ad28-156c157d898" 
    Last-Modified: Thu, 25 Aug 2016 10:56:15 GMT 
    Origin: * 
    X-Powered-By: Express