Javascript Vue.js文件上载上的选项404

Javascript Vue.js文件上载上的选项404,javascript,node.js,express,vue.js,axios,Javascript,Node.js,Express,Vue.js,Axios,我已经在我的express应用程序中定义了cors,大多数路线都没有这个问题。但是,对于我用来上传图像的特定组件: <input type="file" class="form-control" @change="imageChagned"> <div @click="postPhoto">Upload Photo</div> 我在浏览器中遇到此错误: OPTIONS http://127.0.0.1:3000/profile/addphoto 40

我已经在我的
express
应用程序中定义了cors,大多数路线都没有这个问题。但是,对于我用来上传图像的特定组件:

<input type="file" class="form-control" @change="imageChagned">

<div  @click="postPhoto">Upload Photo</div>  
我在浏览器中遇到此错误:

OPTIONS http://127.0.0.1:3000/profile/addphoto 404 (Not Found)
dispatchXhrRequest @ xhr.js?b50d:178
xhrAdapter @ xhr.js?b50d:12
dispatchRequest @ dispatchRequest.js?5270:59
Promise.then (async)
request @ Axios.js?0a06:51
Axios.(anonymous function) @ Axios.js?0a06:71
wrap @ bind.js?1d2b:9
postPhoto @ AddPhoto.vue?0625:242
invoker @ vue.runtime.esm.js?2b0e:2023
fn._withTask.fn._withTask @ vue.runtime.esm.js?2b0e:1822
addphoto:1 Access to XMLHttpRequest at 'http://127.0.0.1:3000/profile/addphoto' from origin 'http://127.0.0.1:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
以下是接收post的路由器:

  router.post('/addphoto',  checkAuth, (req, res)=> {

    console.log('add photo post received');
       let filename = Math.floor(Math.random() * 100000);
  let dir = './uploads/' + req.user.id;
    if (!fs.existsSync(dir)){
        fs.mkdirSync(dir);
    }   
    base64String = req.body.file;
    let str = base64String.split(';base64,');
    let base64Image = str[1];
    let extRaw = str[0];
    let ext = extRaw.split('/')[1].toLowerCase();
    let extArray = ['jpg', 'jpeg', 'png', 'gif'];
    const buffer = Buffer.from(base64String.substring(base64String.indexOf(',') + 1));
    fileSizeMB = buffer.length / 1e+6
    //Check file extension
    if (!extArray.includes(ext)) {
      res.status(403).json({msg: 'error: file extention not allowed'})
      return
    };

    //Check file size
    if(fileSizeMB > 5) {
      res.status(403).json({msg: 'error: file is too large'})
      return
    }

    let imgId =  filename + '.' + ext; 
    let filePath = dir + "/" + imgId;
    fs.writeFile( filePath, base64Image, {encoding: 'base64'}, function(err) {
      });
      const photoFields = {};
      photoFields.photos = []
      let p = {}

      p.imgId =  imgId  ;
      p.visible = 'all'
    User.findOne({ _id: req.user.id }).then(user => {
      if (!user.images.profileImg.length > 0 ) {
        user.images.profileImg = p.imgId;
      }

      user.images.photos.push(p);
      user.save().then(()=>{
        console.log('new photo is saved');
        res.json({ images: user.images });
      }).catch(err =>
      console.log(err)
      );

    });


  });
这个问题困扰了我好几个小时,所以非常感谢你的提示来解决它


奇怪的是,这个组件工作得很好,但在我对代码做了一些小的修改之后,它就停止了工作

如错误所示,对于方法
选项
,没有名为
/profile/addphoto
的端点。您发布的路由不会被
OPTIONS
请求击中,因为您指定它只接收
POST

选项
请求用于让请求者知道如何允许其使用您的API,因此只需在设置cors头后返回状态200即可。我通常通过添加此命令来截获对API发出的任何
选项
请求。只需将其添加到路由开始的某个位置:

app.use(function(req, res, next) {
    res.set('Access-Control-Allow-Origin', '*');
    res.set('Access-Control-Allow-Headers', 'Origin, Accept, Content-Type, X-Requested-With, auth_token, X-CSRF-Token, Authorization');
    res.set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, DELETE, PUT, PATCH');
    res.set('Access-Control-Allow-Credentials', 'true');

    // intercept OPTIONS method
    if (req.method === 'OPTIONS') {
        return res.status(200).end();
    }

    next();
});

变化是什么?你做了什么?我对模式做了一些更改,但对服务器没有。有太多的更改我无法回溯。谢谢你的提示,但是在应用了你的
应用程序之后。现在我收到
帖子,请使用
http://127.0.0.1:3000/profile/addphoto 404(未找到)
。有什么想法吗?如果看不到你的整个路线,很难判断。我的路线很简单:
app.use('/users',users);应用程序使用('/profile',profiles)仅用于记录。清除缓存后,
404
POST错误消失。奇怪!
app.use(function(req, res, next) {
    res.set('Access-Control-Allow-Origin', '*');
    res.set('Access-Control-Allow-Headers', 'Origin, Accept, Content-Type, X-Requested-With, auth_token, X-CSRF-Token, Authorization');
    res.set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, DELETE, PUT, PATCH');
    res.set('Access-Control-Allow-Credentials', 'true');

    // intercept OPTIONS method
    if (req.method === 'OPTIONS') {
        return res.status(200).end();
    }

    next();
});