Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用express运行vue时出现cors错误_Node.js_Express_Vue.js - Fatal编程技术网

Node.js 使用express运行vue时出现cors错误

Node.js 使用express运行vue时出现cors错误,node.js,express,vue.js,Node.js,Express,Vue.js,我在express服务器上运行我的Vue应用程序(nodejs在端口60702上运行),如下所示: 用户路由器是: router.post('/login', async (req, res) => { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-Wi

我在express服务器上运行我的Vue应用程序(nodejs在端口60702上运行),如下所示:

用户路由器是:

router.post('/login', async (req, res) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods', 'GET,POST");

  let compareUser = await db.query('SELECT * FROM app_users WHERE username=? LIMIT 1', [req.body.username]); // use db.query() to retrieve the password
  if (compareUser.length < 1) // compareUser is an array with at most one item
    res.sendStatus(403);
  let valid = bcrypt.compareSync(req.body.password, compareUser[0].password);
  if (!valid)
    res.sendStatus(403);

  let user = new User(compareUser[0]);
  const token = jwt.sign({
    user
  }, nconf.get('jwtToken'), {
    expiresIn: '14d'
  });
  Object.assign(user, {
    token
  });
  res.json(user);
});
Axios:

const apiClient = axios.create({
  baseURL: `https://192.168.51.47:60702`,
  withCredentials: false, // This is the default
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json'
  }
})

export default {
  // user Endpoints
  getUser(email) {
    return apiClient.get(`/user/${email}`)
  },
  registerUser(user) {
    return apiClient.post(`/user/register`, user)
  },
  loginUser(user) {
    return apiClient.post(`/user/login`, user)
  },
但即使我包括cors,我也会得到:

已阻止跨源(跨源)请求:同一源规则 禁止在上读取外部资源 . (原因:CORS请求失败)

vue中的axios调用还具有正确的baseUrl和端口


我在/user/login with Postman处检查了到后端的POST请求,也得到了正确的请求。

通过使用重新创建dist文件夹解决了这个问题

npm运行构建

感谢@Dan的帮助

不要使用apiClient。使用完整url进行get,重建应用程序, 删除旧的dist文件夹,加载后按CTRL+F5刷新。事实上,放一个 在url的末尾显示“?”,并确保在Chrome标题中看到它


评论不用于扩展讨论;这段对话已经结束。
module.exports = {
  baseUrl: process.env.NODE_ENV === 'production' ? '/vue' : '/',
  devServer: {
    port: 60702,
    https: true,
    disableHostCheck: true
  }
};
const apiClient = axios.create({
  baseURL: `https://192.168.51.47:60702`,
  withCredentials: false, // This is the default
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json'
  }
})

export default {
  // user Endpoints
  getUser(email) {
    return apiClient.get(`/user/${email}`)
  },
  registerUser(user) {
    return apiClient.post(`/user/register`, user)
  },
  loginUser(user) {
    return apiClient.post(`/user/login`, user)
  },