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

标签 javascript node.js express parse-platform

我想有人曾经使用过 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/

相关文章:

javascript - 我试图在向该对象添加新属性之前将对象推送到数组,但它直接更新数组

javascript - chartJS 中的动态颜色基于数组中的条目

javascript - 在加载和调整大小时更改 Div 宽度

c++ - 如何从插件中的单独 C++ 线程调用发射器回调?

javascript - 无法使用 MySQL 和 ReactJS 显示行

javascript - 如何检查 d3 中的 NaN 值?

node.js - 如何查找搜索/查找 npm 包

json - 如何使用 Advanced REST Client 或 Postman 测试 Express/node REST API 后端?

node.js - Nodejs、express、移动 Passport

javascript - 带有 Express 服务器的 create-react-app 未在 IE 中呈现