Nb
Study
.com
🔍 请输入搜索关键字

npm报错:request to https://registry.npm.taobao.org failed, reason certificate has expired

最新一个老项目,在使用 npm install 时, 总是报错:

npm报错:request to https://registry.npm.taobao.org failed, reason certificate has expired

很明显原因是淘宝镜像过期了!

通过npm config get registry 查询本地的npm镜像:

bash 复制代码
apple@AppledeMacBook-Pro h5 % npm config get registry                                              
https://registry.npmmirror.com/

发现明明已经切换到新的镜像了啊,为什么还是报错呢?

原因是项目本地的package-lock.json 文件导致的,删除即可。

axios的withCredentials到底是什么意思?

通常我们使用以下代码创建一个axios实例。

javascript 复制代码
const axiosInstance = axios.create({
  baseURL:import.meta.env.AXIOS_BASE_URL as string,
  withCredentials: true,
  timeout: 55000,
});

那上述代码中的withCredentials 是什么意思呢?

withCredentialsXMLHttpRequest 对象的一个属性。

axios也是XMLHttpRequest的封装,当然最新的axios版本底层也支持fetch了。

withCredentials用于指示是否应该允许跨域请求携带凭据(如Cookies、HTTP认证信息、客户端SSL证明等)。当设置为 true 时,它允许跨域请求发送凭据,但同时也要求服务器端必须正确响应CORS(跨源资源共享)策略

详解 withCredentials:

1. 默认值:withCredentials 的默认值是 false。

这意味着,除非显式设置为 true,否则跨域请求不会携带凭据。

当你设置 XMLHttpRequest 的 withCredentials 属性为 true 时,你告诉浏览器在发送跨域请求时携带凭据。例如:

javascript 复制代码
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

2. 服务器端响应:

当 withCredentials 设置为 true 时,服务器必须在其CORS响应头中包含 Access-Control-Allow-Credentials: true,以允许浏览器接受带有凭据的请求。同时,Access-Control-Allow-Origin 不能设置为通配符 *,而必须指定明确的源

3. 安全性考虑:

允许跨域请求携带凭据可能会带来安全风险,因为它允许第三方网站访问敏感的用户数据。因此,只有当你完全信任目标源,并且需要在请求中包含用户凭据时,才应该将 withCredentials 设置为 true。

4. 现代浏览器和Fetch API:
在现代浏览器中,fetch API 也支持跨域请求。当使用 fetch 发起跨域请求时,可以通过设置 credentials: 'include' 来达到与 XMLHttpRequest 中 withCredentials: true 相同的效果。

七牛云前端表单直传文件提示Access-Control-Allow-Origin跨域如何解决?

使用过七牛云的小伙伴都知道,七牛云支持前端通过表单直传,只需要一个token即可。

通过如果我们使用 element plus 等UI框架自带的上传组件,只需简单配置 actiondataheader等属性即可,框架自动帮我们进行上传。

但是如果需要自定义一个文件上传组件,有可能会提示跨域。
例如,我们可以使用axios进行自定义上传:

javascript 复制代码
const axiosInstance = axios.create({
  // 问题就在这里
  withCredentials: true
  timeout: 150000,
});

const uploadFile: RequstType = async (url, params) => {
  const config = {
    headers: { "Content-Type": "multipart/form-data" },
  };
  return await axiosInstance.post(url, params,config);
};
const fd = new FormData();
// 选择的文件
fd.append("file", file);
// 后端返回的上传凭证
fd.append("token", "token");
uploadFile("https://upload.qiniup.com/",fd)

错误解读

上述代码看似没有问题,但是就是会提示跨域。

我们分析以下报错的提示:

Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

错误信息的意思是:

  1. Preflight request:这是一个预检请求,浏览器在发送实际请求之前,会先发送一个 OPTIONS 请求,以确定是否安全可以发送请求。
  • Access-Control-Allow-Origin header:这个响应头必须包含一个明确的允许访问的源,而不是通配符 *。
  1. The request's credentials mode is 'include':这意味着请求包含了凭据(如Cookies或HTTP认证信息),因此浏览器要求服务器明确允许来自你的源的请求。
  2. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute:这表明请求是通过 XMLHttpRequest 发起的(此处为axios),并且 withCredentials 属性被设置为 true。

解决方案

很明显,我们无法对七牛云的服务端进行配置。那只能通过前端配置 withCredentials 即可.
修改代码如下:

javascript 复制代码
const uploadFile: RequstType = async (url, params) => {
  const config = {
    headers: { "Content-Type": "multipart/form-data" },
    // 不携带cookie即可
    withCredentials: false
  };
  return await axiosInstance.post(url, params,config);
};