首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重写/重定向Meteor中的默认页面以指向/public/index.html

如何重写/重定向Meteor中的默认页面以指向/public/index.html
EN

Stack Overflow用户
提问于 2016-06-17 20:04:48
回答 1查看 203关注 0票数 0

我使用Meteor作为我的ionic+angular webApp的后端。我正在使用meteor-up部署应用程序。我已经将我的整个应用程序放在Meteor /public文件夹中,当我像这样访问它时,它可以找到它:

http://localhost:3000/index.html

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

而不会丢失我的Meteor服务器

EN

回答 1

Stack Overflow用户

发布于 2016-06-17 20:50:59

以下是完整的解决方案:

代码语言:javascript
复制
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();
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37881148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档