如何重写/重定向Meteor中的默认页面以指向/public/index.html [英] How can I rewrite/redirect the default page in Meteor to point to /public/index.html

查看:225
本文介绍了如何重写/重定向Meteor中的默认页面以指向/public/index.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Meteor作为我的ionic+angular webApp的后端.我正在使用meteor-up部署应用程序.我已将整个应用程序放在Meteor /public文件夹中,当我按如下方式访问它时,它可以正常工作:

I am using Meteor as the backend to my ionic+angular webApp. I'm deploying the app using meteor-up. I have put my entire app in the Meteor /public folder and it works find when I access it like this:

http://localhost:3000/index.html

如何设置/重写/重定向Meteor默认页面,以便可以从以下页面加载同一页面:

How can I set/rewrite/redirect the Meteor default page so I can load the same page from:

http://localhost:3000/http://localhost:3000/myApp

不丢失我的Meteor服务器

推荐答案

以下是完整的解决方案:

Here is the complete solution:

fs = Npm.require('fs');
crypto = Npm.require('crypto');
WebApp.connectHandlers.use("/", function(req, res, next) {
  var data, filepath;
  if (req.method !== 'GET') {
    return next();
  }
  filepath = process.env.PWD + '/public/index.html';

  // serve default file, with eTag, 
  // i.e. http://localhost:3000/
  fs.readFile(filepath, function(err, buf) {
    var eTag;
    eTag = crypto.createHash('md5').update(buf).digest('hex');
    if (req.headers['if-none-match'] === eTag) {
      res.writeHead(304, 'Not Modified');
      return res.end();
    }
    res.writeHead(200, {
      'ETag': eTag,
      'Content-Type': 'text/html'
    });
    return res.end(buf);
  });
  return;

  // serve default file, without eTag or caching headers
  // i.e. http://localhost:3000/
  data = fs.readFileSync(filepath);
  res.writeHead(200, {
    'Content-Type': 'text/html'
  });
  res.write(data);
  return res.end();

  // redirect to default file
  // i.e. http://localhost:3000/index.html
  res.writeHead(301, {
    'Location': '/index.html'
  });
  return res.end();
});

这篇关于如何重写/重定向Meteor中的默认页面以指向/public/index.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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