繁体   English   中英

将所有URL重定向到Parse.com上的index.html

[英]Redirect all URLs to index.html on Parse.com

我想有些人在使用Parse.com网站托管或玩过单页应用程序。 我有一个webapp,它是带有主要index.html文件的SPA。 我在应用程序中使用pushState,并且希望将用户重定向到index.html,无论他们在我的网站上输入一些URL,例如“ / profile”,“ / projects”等等。

我使用Express在本地计算机上对其进行了配置,但是由于Parse具有自己的规则和环境,我认为我需要一个适用于它的特定解决方案。

请指教。

让Express将您的index.html呈现为EJS视图。 将您的index.html复制到cloud/views/index.ejs或将其cloud/views/index.ejs链接到同一位置。

// 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();

我一直在寻找这个问题的答案,结果发现Parse.comGoogle Group似乎比Stack Overflow上的解析器更活跃。 在这里找到这个答案。 用您的网址替换“ 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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM