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

时间:2016-06-17 12:04:48

标签: node.js meteor meteor-up

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

/public

如何设置/重写/重定向http://localhost:3000/index.html默认页面,以便我可以从以下位置加载相同的页面:

Meteorhttp://localhost:3000/

不会丢失我的http://localhost:3000/myApp服务器

1 个答案:

答案 0 :(得分:0)

以下是完整的解决方案:

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