当我点击登录链接时,我转到/login
。看起来像这个截图的链接:
请注意我的Angular 2应用程序发出请求后给出的响应Session not found
。检查用户是否
登录。这一切都很好。
但是当我刷新页面时,我收到以下错误Cannot GET /login
链接到屏幕截图:
要解决该错误,我在Express应用中添加了以下内容:
//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'));
});
现在我可以刷新页面而不会收到错误Cannot GET /login
。但现在的问题是我得到了以下内容
错误:Unexpected token < in JSON at position 0
当我的Angular 2应用想要检查用户是否已登录时。
这也是它的截图:
相反,我应该将错误作为回复Session not found
。但不幸的是,这并没有发生。
以下是我的Express应用程序的代码:
app.js:
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;
index.js:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function (req, res, next) {
res.render('index.html');
});
module.exports = router;
这里还有我的项目链接到屏幕截图的文件夹结构截图:
此外,还有一个指向Github上我的存储库的链接,以防您想要查看我的项目链接的更多代码: https://github.com/superzaky/node-portfolio-zaky/tree/refresh-problem
任何人都知道如何解决这个问题?
答案 0 :(得分:2)
当您在/login
刷新页面时,会在后端调用此URI,因为您可以看到您不支持在那里处理该路由。您输入的代码是正确的,但您必须将其放在所有路由处理程序下面。所以地点:
app.route('/*').get(function(req, res) {
return res.sendFile(path.join(__dirname, 'public/index.html'));
});
下面的行:
app.use('/', index);
app.use('/api/auth', require('./controllers/AuthController'));
app.use('/api/v1/', projects);
现在将为后端未明确定义的每个URI加载前端,这使您仍然可以使用后端API调用。只需确保前端和后端不使用相同的URI。