Cannot GET /login
<p>When I click on the login link then I go to the to <code>/login</code>. Which looks like this link to screenshot:</p>

<p><a href="https://i.stack.imgur.com/MoFvr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MoFvr.png" alt="enter image description here"></a></p>

<p>Notice the response <code>Session not found</code> which was given to my Angular 2 app after it made a request. To check if a user
is logged in. This is all working good.</p>

<p>But when I refresh the page then I get the following error <code>Cannot GET /login</code> link to screenshot:</p>

<p><a href="https://i.stack.imgur.com/gIryi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gIryi.png" alt="enter image description here"></a></p>

<p>To fix that error I had added the following in my Express app:</p>

<pre><code>//We use this to avoid the error: Cannot GET /login
//but if we use this, then we can't receive a session on the Angular 2 app..
app.route('/*').get(function(req, res) { 
    return res.sendFile(path.join(__dirname, 'public/index.html')); 
});
</code></pre>

<p>Now I can refresh the page without getting the error <code>Cannot GET /login</code>. But the problem now is that I'm getting the following
error: <code>Unexpected token &lt; in JSON at position 0</code> when my Angular 2 app wants to check if a user is logged in.
Here is also a screenshot of it:</p>

<p><a href="https://i.stack.imgur.com/dPcXO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dPcXO.png" alt="enter image description here"></a></p>

<p>Instead I should get the error as a response <code>Session not found</code>. But unfortunately this didn't happened.</p>

<p>Here is the code of my Express app:</p>

<p>app.js:</p>

<pre><code>var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var projects = require('./routes/projects');
var app = express();
var mongoose = require('mongoose');
var config = require('./config/config');
var session = require('express-session');

if (app.settings.env === "development") {
    mongoose.connect(config.getDbPath(app.settings.env));
}

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
    // we're connected!
});

// view engine setup
app.set('views', path.join(__dirname, 'views'));

app.use(function (req, res, next){
    res.setHeader('Access-Control-Allow-Origin', "*");
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type');
    next();
});

app.use(session({
    secret: 'keyboard cat',
    resave: true,
    saveUninitialized: true
}));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));

app.use(express.static(path.join(__dirname, 'public')));

//We use this to avoid the error: Cannot GET /login
//but if we use this, then we can't receive a session on the Angular 2 app..
//app.route('/*').get(function(req, res) { 
//    return res.sendFile(path.join(__dirname, 'public/index.html')); 
//});

// we are mapping the routes to end points.
app.use('/', index);
app.use('/api/auth', require('./controllers/AuthController'));
app.use('/api/v1/', projects);

// TODO: catch 404 and forward to error handler

var port = 3000;

if(process.env.NODE_ENV === "test") port = 3002;

var server = app.listen(port, function () {
    var host = 'localhost';
    var port = server.address().port;
    console.log('App listening at http://%s:%s', host, port);
});
module.exports = app;
</code></pre>

<p>index.js:</p>

<pre><code>var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function (req, res, next) {
    res.render('index.html');
});
module.exports = router;
</code></pre>

<p>Here is also a screenshot of my folder structure of my project link to screenshot:</p>

<p><a href="https://i.stack.imgur.com/QSGgw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QSGgw.png" alt="enter image description here"></a></p>

<p>Also here is a link to my repository on Github in case you want to see more of the code of my project link:
<a href="https://github.com/superzaky/node-portfolio-zaky/tree/refresh-problem" rel="nofollow noreferrer">https://github.com/superzaky/node-portfolio-zaky/tree/refresh-problem</a></p>

<p>Anyone that maybe knows how I can solve this?</p>