简体   繁体   中英

Redirect all URLs to index.html on Parse.com

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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