Giter VIP home page Giter VIP logo

mern-app's People

Contributors

biosboy avatar

Stargazers

 avatar

mern-app's Issues

Heroku-MERN. How to connect to the website server throw :PORT?

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?

And the main question - how to get access to the server pages like in the http://localhost:3016 on the Heroku?

What I did already:

I had set the port and env.PORT of my app for Heroca server.js file:

const port = process.env.API_PORT || 3016;
app.listen(port, () => {
console.log(Server running on port ${port});
});
I setup my Procfile for Heroca

React: react-scripts start
Server: nodemon ./Server/server.js
I have access to the my static app and all pages normally by URL - http://my-project.heroku.com without port - 3016.

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.

When I try to go like http://my-project.heroku.com:3016 my page are start load infinity. If I try to go like like http://my-project.heroku.com:80, the page reload immideantly without the port 80.

My server.js file:

'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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.