Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

node.js - How can I rewrite/redirect the default page in Meteor to point to /public/index.html

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

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

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

without losing my Meteor server

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...