I have deployed my backend on Heroku and works fine as I send a request from POSTMAN.
I have deployed the My react app on Heroku as well.
But I can not get a response from the backend server. Please help
This is the Heroku log detail from the backend.
2020-12-30T16:45:34.688348+00:00 heroku[web.1]: Starting process with command `node index.js`
2020-12-30T16:45:37.852435+00:00 app[web.1]: Server is listening on 34248
2020-12-30T16:45:37.870603+00:00 heroku[web.1]: State changed from starting to up
2020-12-30T16:45:38.000000+00:00 app[api]: Build succeeded
2020-12-30T16:48:18.862763+00:00 heroku[router]: at=info method=OPTIONS path="/api/auth/signup" host=book-share-server-yp.herokuapp.com request_id=4d909f22-7fc3-4273-b010-4395adae20b7 fwd="184.146.219.105" dyno=web.1 connect=1ms service=10ms status=204 bytes=391 protocol=https
2020-12-30T16:48:18.857570+00:00 app[web.1]: OPTIONS /api/auth/signup 204 2.315 ms - 0
Axios Instance fro Api call
import axios from "axios";
const instance = axios.create({
baseURL:"https://book-share-server-yp.herokuapp.com/api",
withCredentials: true,
});
instance.defaults.headers.common['Content-Type'] ="application/json";
export default instance;
Signup action
export const signUpUser = (formData)=>{
return dispatch => {
// /signup
axiosInstance.post("/auth/signup",formData)
.then(res =>{
getUser(dispatch);
} )
.catch(error => {
signUpUserFail(dispatch, error.response.data.message)
})
}
}
backend index.js
const express = require("express");
const morgan = require("morgan");
const mongoose = require("mongoose");
const app = express();
const user = require("./routes/users");
const auth = require("./routes/auth");
const books = require("./routes/books");
const notFound = require("./middleware/notFound");
const errorHandler = require("./middleware/errorHandler");
const {authCheck} = require("./middleware/authCheck");
const cookieParser = require("cookie-parser");
const cors = require("cors");
require("dotenv").config();
//DB Connection
mongoose.connect(process.env.MONGO_URI,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
}).catch(error =>{
console.log(error)
throw Error("DB connection problem")
});
const port = process.env.PORT||8004;
app.use(express.json());
app.use(cookieParser());
app.use(morgan("dev"));
app.use(cors({
origin:"https://book-share-client-yp.herokuapp.com/",
credentials:true
}))
app.use("/api/user",authCheck,user);// add authcheck middleware
app.use("/api/auth",auth)
app.use("/api/books",authCheck, books); // add authcheck middleware
app.use( notFound);
app.use( errorHandler);
app.listen(port, ()=>{
console.log(`Server is listening on ${port}`)
})