简体   繁体   English

在 Heroku 上部署后出错(应用程序错误)

[英]Error after deploying (Applicaton error) on Heroku

Intro介绍

I had made a Login form.我做了一个登录表单。

I had deployed the frontend on Netlify here and the backend on Heroku here .我在 Netlify 上部署了前端,在Heroku上部署后端。 This is my backend logs https://dashboard.heroku.com/apps/my-notebook-mohit/logs这是我的后端日志https://dashboard.heroku.com/apps/my-notebook-mohit/logs

My index.js is我的 index.js 是

const connectToMongo = require('./db');
const express = require('express');


connectToMongo();
var cors = require('cors')
const app = express()
const port = 5000

//to use req body 
app.use(cors())
app.use(express.json())

//Available routes
app.use('/api/auth',require('./routes/auth'));
app.use('/api/notes',require('./routes/notes'));

app.listen(process.env.PORT || port , () => {
  console.log(`my-notebook backend listening at http://localhost:${process.env.PORT || port}`)
})


LoginPage.js登录页面.js

import React, { useState } from "react";
import { useHistory } from "react-router-dom";
import imgpath from "../assets/notepic.jpg";
import { motion } from "framer-motion";

const Login = (props) => {
  let history = useHistory();

  const [credentials, setCredentials] = useState({ email: "", password: "" });

  const onChange = (e) => {
    setCredentials({ ...credentials, [e.target.name]: e.target.value });
    //input mei value typed ho sake,jaise jaise value change ho vese-vese note me set ho jaye
  };

  const goToSignup = () => {
    history.push("/signup");
  };
  
  const handleSubmit = async (e) => {
    e.preventDefault();
    const response = await fetch("http://localhost:5000/api/auth/login", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        email: credentials.email,
        password: credentials.password,
      }),
    });
    const json = await response.json();
    if (json.success === true) {
      //storing the authtoken
      localStorage.setItem("token", json.authToken);
      props.showAlert("User logged in successfully", "info");
      history.push("/");
    } else {
      props.showAlert("invalid Credentials", "danger");
    }
    console.log(json);
  };

  return (
   .....
   <form onSubmit={handleSubmit} className="login-form">
           .....
           ....
   </form>
  ...
  ...

};

export default Login;

Problem I facing我面临的问题

When I make a POST request on the login button from frontend deployed on Netlify , it goes to http://localhost:5000/api/auth/login as it makes the fetch request there only, hence shows error.当我从部署在 Netlify 上的前端的登录按钮上发出 POST 请求时,它会转到http://localhost:5000/api/auth/login因为它只在那里发出获取请求,因此显示错误。

error ss错误ss

What I think?我怎么想?

The reason I could think is that Heroku is dynamically assigning the any other port ie 49004 when I check from Heroku Logs and my request is sent over port 5000 .我能想到的原因是,当我从 Heroku 日志检查并且我的请求通过端口 5000发送时,Heroku 正在动态分配任何其他端口,即 49004 。

How do I add the dynamic port to the fetch request I made?如何将动态端口添加到我提出的获取请求中?

I went through your code, you've used const port = 5000 in your nodejs backend.我浏览了您的代码,您在 nodejs 后端使用了const port = 5000

This must be replaced with const port = process.env.PORT || 5000这必须替换为const port = process.env.PORT || 5000 const port = process.env.PORT || 5000

This is because heroku dynamically assigns a port number to the running application when it's deployed, so you can't set the port to a fixed number.这是因为 heroku 在部署时会为正在运行的应用程序动态分配端口号,因此您无法将端口设置为固定号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM