我想有人曾经使用过 Parse.com 网站托管单页应用程序。我有一个 web 应用程序,它是一个带有主 index.html 文件的 SPA。我在应用程序中使用pushState,并且希望用户在我的网站上输入某些网址(例如“/profile”、“/projects”等)时被重定向到index.html。
我在本地计算机上使用 Express 配置了它,但由于 Parse 有自己的规则和环境,我认为我需要一个与其配合使用的特定解决方案。
请指教。
最佳答案
让 Express 渲染您的 index.html
,就像它是 EJS
View 一样。
将 index.html
复制到 cloud/views/index.ejs
或将其符号链接(symbolic link)到同一位置。
// app.js
var express = require('express');
var app = express();
app.set('views','cloud/views');
app.set('view engine', 'ejs');
app.get('/*', function(req, res) {
res.render('index.ejs');
});
app.listen();
我一直在寻找这个问题的答案,结果是 Google Group for Parse.com似乎比 Stack Overflow 上的解析器更活跃。找到了这个答案here 。将“YOUR_URL”替换为您的网址。
***** 编辑 ******
我现在在同一个 Google 群组中使用第二个答案,因为它似乎效果更好:
var express = require('express');
var app = module.exports = express();
app.express = express;
//push state interceptors
var pushUrlPrefixes = [""];
var index = null;
function getIndex(cb) {
return function(req, res) {
Parse.Cloud.httpRequest({
url: 'YOUR_URL',
success: function(httpResponse) {
index = httpResponse.text;
cb(req, res);
},
error: function(httpResponse) {
res.send("We're very busy at the moment, try again soon.");
}
});
}
}
pushUrlPrefixes.forEach(function(path) {
app.get("/"+path+"*", getIndex(function(req, res) {
res.set('Content-Type', 'text/html');
res.status(200).send(index);
}));
});
//redirect requests here, instead of parse hosting
app.listen();
关于javascript - 将所有 URL 重定向到 Parse.com 上的 index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30754737/