【问题标题】:405 Not Allowed in Production405 不允许在生产中使用
【发布时间】:2021-11-30 10:27:05
【问题描述】:

link to repo
link to bug report

我一直在关注Lama Dev's Social Networking Tutorial 但是在 docker 容器中运行它,他在教程中没有这样做。其中一个容器是 Traefik,它充当 80 和 443 的网关,还有一个充当管理面板的网关,但我还没有为 API 打开 8800,因为我知道 React 仍然应该在 docker 的默认网络中看到它,我不确定 405 是来自 API 还是仍然在 React 中,我在 405 上读到的内容说它通常在客户端中,并且记录错误的行在 React 中,但它在失败的调用中,所以可能 405 消息是 Axios 响应,表示不允许连接。我不确定还可以尝试更改什么?

它似乎在本地开发中运行良好,但在生产中却不行,它通常在https:geoLARP.com,如果它关闭了几分钟,我可能正在尝试在服务器上重建,它很快就会恢复。

我还想知道是否需要删除 CORS,他在 repo 中有它们,但从未在视频中提及它们..

我认为从 devprod 的唯一环境变量是 LOCAL_HOST,我将其更改为 geolarp.com,我也应该包括HTTP:// 吗?

export DEV_MAIL=janeDOE@mail.com
export LOCAL_HOST=localhost
export MongoDB_PASSWORD=butterzCUPz
export MongoDB_USERNAME=janeDOE
export WORDPRESS_DB_HOST=db
export WORDPRESS_DB_NAME=exampledb
export WORDPRESS_DB_PASSWORD=examplepass
export WORDPRESS_DB_USER=exampleuser
docker-compose up -d --build
docker-compose down -v --remove-orphans

我开始尝试通过 PostMan,但目前,我还没有将 API 向公众开放,我希望 React 只需将代理更改为 API 容器即可在 docker 默认网络上看到它。所以我在.env 文件中将localhost:8800 更改为mernlama:8800

Regist.jsx

import axios from "axios";
import { useRef } from "react";
import { useHistory } from "react-router";
import "./register.css";

export default function Register() {
  const username = useRef();
  const email = useRef();
  const password = useRef();
  const passwordAgain = useRef();
  const history = useHistory();

  const handleClick = async (e) => {
    e.preventDefault();
    // console.log(email.current.value);
    if (passwordAgain.current.value !== password.current.value) {
      passwordAgain.current.setCustomValidity('PassWords do not Match');
    } else {
      const user = {
        username: username.current.value,
        email: email.current.value,
        password: password.current.value,
      };
      try {
        await axios.post("/auth/register", user);
        history.push("/login");
      } catch (err) {
        console.log(err);
      }
    }
  };
  return (
    <div className="login">
      <div className="loginWrapper">
        <div className="loginLeft">
          <h3 className="loginLogo">geoLARP</h3>
          <span className="loginDesc">
            GeoLocated
          </span>
          <span className="loginDesc">
            Live Action Role Playing
          </span>
        </div>
        <div className="loginRight">
          <form className="loginBox" onSubmit={handleClick} >
            <input
              placeholder="Username"
              required
              className="loginInput"
              ref={username}
            />
            <input
              placeholder="please use a disposable Email"
              type='email'
              required
              className="loginInput"
              ref={email}
            />
            <input
              placeholder="Password"
              type='password'
              required
              minLength="6"
              className="loginInput"
              ref={password}
            />
            <input
              placeholder="Password Again"
              type='password'
              required
              className="loginInput"
              ref={passwordAgain}
            />
            <button className="loginButton" type='submit' >Sign Up</button>
            <button className="loginRegisterButton">
              Log into Account
            </button>
          </form>
        </div>
      </div>
    </div>
  );
}

expressAPI/routes/auth.js

const router = require('express').Router();
const User = require('../models/User');
const bcrypt = require('bcrypt');

// REGISTER
router.post('/register', async (req, res) => {

  try {
    // generate a salty password
    const salt = await bcrypt.genSalt(10);
    const hashedPassword = await bcrypt.hash(req.body.password, salt);

    // create a new user
    const newUser = new User({
      username: req.body.username,
      email: req.body.email,
      password: hashedPassword,
    });

    // save the user and respond
    const user = await newUser.save();
    console.log(user);
    res.status(200)
      // .json({ message: 'Registered User with API AUTH Route', user: user._id });
      .json(user);
  } catch (err) {
    res.status(500).json(err);
  }

}); // GET /api/auth


// LOGIN
router.post('/login', async (req, res) => {
  try {
    // find the user
    const user = await User.findOne({ email: req.body.email });

    // check if the user exists
    !user && res.status(404).json('User not found');


    // check if the password is correct
    const validPassword = await bcrypt.compare(req.body.password, user.password);
    !validPassword && res.status(400).json('Invalid Password');

    // respond with the user
    res.status(200).json(user);
  } catch (err) {
    res.status(500).json(err);
  }
}); // GET /api/auth/login

module.exports = router;
// export default router;

【问题讨论】:

    标签: node.js reactjs docker express


    【解决方案1】:

    正如我在您的Register.jsx 中看到的,您正在请求后端服务器,例如

    await axios.post("/auth/register/",user ) 
    

    您必须准确指定您的其余 API。例如,如果您的后端 API 是 localhost:8080/api/users/auth/register,您必须在请求中指定。

    解决办法:

    .env上创建一个变量:

    REACT_APP_BACKEND_URL = your backend url ( localhost:8080/api/users )
    

    然后拨打Register.jsx:

    await axios.post(process.env.REACT_APP_BACKEND_URL+"auth/register" , user ) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 2023-03-24
      • 2011-09-01
      • 2012-06-29
      • 2013-03-13
      • 2016-03-08
      • 2014-09-19
      相关资源
      最近更新 更多