在Parse.com上将所有URL重定向到index.html [英] Redirect all URLs to index.html on Parse.com

查看:106
本文介绍了在Parse.com上将所有URL重定向到index.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有些人在Parse.com网站托管工作或玩过单页应用程序。我有一个webapp,它是一个带有主index.html文件的SPA。我在应用程序中使用pushState,我希望用户可以在我们网站上输入某个网址时重定向到index.html,例如'/ profile','/ projects'等等。



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



<请提供建议。

解决方案

让Express呈现您的 index.html 好像它是一个 EJS 视图。
index.html 复制到 cloud / views / index.ejs 或sym-link to同一个地方。

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

我一直在寻找这个问题的答案FOREVER,结果 Google Group for Parse.com 似乎比Stack Overflow上的Parsers更活跃。我的回答是这里。将YOUR_URL替换为您的网址。



******* 编辑 *******



我现在在同一个Google网上使用第二个答案,因为它看起来效果更好:

  var express = require('express'); 
var app = module.exports = express();
app.express = express;

//推送状态拦截器
var pushUrlPrefixes = [];
var index = null;

函数getIndex(cb){
返回函数(req,res){
Parse.Cloud.httpRequest({
url:'YOUR_URL',
success:function(httpResponse){
index = httpResponse.text;
cb(req,res);
},
error:function(httpResponse){
res.send(我们现在非常忙,很快再试一次。);
}
});
}
}

pushUrlPrefixes.forEach(函数(路径){
app.get(/+ path +*,getIndex(function(req, res){
res.set('Content-Type','text / html');
res.status(200).send(index);
}));
});

//在这里重定向请求,而不是解析托管
app.listen();


I suppose there are people who worked or played with Parse.com site hosting for there single-page apps. I have a webapp which is a SPA with main index.html file. I use pushState in the application and I want users to be redirected to index.html wherever they enter some url on my site, like '/profile', '/projects' and so on.

I configured it on my local machine with Express but as Parse has its own rules and environment, I think I need a specific solution that will work with it.

Please advise.

解决方案

Have Express render your index.html as if it were an EJS view. Either copy your index.html to cloud/views/index.ejs or sym-link it to the same location.

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

I'd been looking for an answer to this question FOREVER, and turns out the Google Group for Parse.com seems more active than Parsers on Stack Overflow. Found this answer here. Replace 'YOUR_URL' with your URL.

******* EDIT *******

I now use the 2nd answer on the same Google Group since it seems to work better:

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

这篇关于在Parse.com上将所有URL重定向到index.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆