So, I have the might be same problem like in this post Axios Api calls in Heroku , but cannot figure out how to solve it, because I'm a very beginner with Heroku.
I have an App, builded on MERN stack. The client side fetch server side by Axios. On the development stage I set the http://localhost:3016 on my client side Axios fetch methods like axios.post('http://localhost:3016/auth/login') for the server side access to get/post. And it's work normally on the local mashine. But after I developed my project on the Heroku servers I cannot get to my server like http://my-project.heroku.com:3016...
So, I understand that I must change my localhost on the something else (my current domain probably), but where and how?
Before all this manipulation I also start my server and react app codes in the Heroce CLI panel and get the right result, that my App is work. The server is also work on the port 3016 but it unaccessed from browser http://my-project.heroku.com:3016.
'use strict'
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const bcrypt = require('bcrypt-nodejs');
const session = require('express-session');
const app = express();
const router = express.Router();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', 'http://mern-app-sviatkuzh.herokuapp.com');
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers');
res.setHeader('Cache-Control', 'no-cache');
next();
});
router.get('/', (req, res) => {
res.json('Hello on the Homepage!');
});
router
.get('/auth/login', (req, res) => {
res.json('Hello on the Homepage!');
})
.post('/auth/login', (req, res, next) => {
if (req.body.logemail && req.body.logpassword) {
User.authenticate(req.body.logemail, req.body.logpassword, function (error, user) {
if (error || !user) {
var err = new Error('Wrong email or password.');
err.status = 401;
console.log('User authentification failed');
return next(err);
} else {
req.session.userId = user._id;
console.log('User authentification sucsessful. User cookies: ', req.session.userId);
return res.redirect('/employers');
}
});
} else {
var err = new Error('All fields required.');
err.status = 400;
return next(err);
}
});
app.use('/', router);
const port = process.env.API_PORT || 3016;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
Node ver - 9.7.1. NPM - 5.6.0. Server - Heroku.