【发布时间】:2019-10-22 18:38:53
【问题描述】:
这是我的第一个 nodejs 邮递员程序。我正在检查 CORS 配置。当我尝试在我的邮递员中运行时完成我的代码后,我得到了错误:
TypeError: Cannot read property 'split' of undefined
<br> at origin (C:\xampp\htdocs\nodepro\recipe\server\src\cors.js:5:34)
<br> at C:\xampp\htdocs\nodepro\recipe\server\node_modules\cors\lib\index.js:219:13
<br> at optionsCallback (C:\xampp\htdocs\nodepro\recipe\server\node_modules\cors\lib\index.js:199:9)
<br> at corsMiddleware (C:\xampp\htdocs\nodepro\recipe\server\node_modules\cors\lib\index.js:204:7)
<br> at Layer.handle [as handle_request] (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\layer.js:95:5)
<br> at trim_prefix (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\index.js:317:13)
<br> at C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\index.js:284:7
<br> at Function.process_params (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\index.js:335:12)
<br> at next (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\index.js:275:10)
<br> at urlencodedParser (C:\xampp\htdocs\nodepro\recipe\server\node_modules\body-parser\lib\types\urlencoded.js:91:7)
<br> at Layer.handle [as handle_request] (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\layer.js:95:5)
<br> at trim_prefix (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\index.js:317:13)
<br> at C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\index.js:284:7
<br> at Function.process_params (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\index.js:335:12)
<br> at next (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\router\index.js:275:10)
<br> at expressInit (C:\xampp\htdocs\nodepro\recipe\server\node_modules\express\lib\middleware\init.js:40:5)
原产地:
function(origin, callback) {
if(process.env.CORS_WHITELIST.split(' ').indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not Allowed By CORS Configuration'));
}
}
我收到错误说未定义拆分。
我什至为 WHITELIST 创建了一个 .env 文件,但我仍然收到错误消息。
我在.env 文件"CORS_WHITELIST=http://localhost:3000" 中给出了下面的书面代码。
【问题讨论】:
-
请缩进你的代码,它很难读