我想将 Meteor 与 React 一起使用来为网站提供服务,但我希望默认主页是普通的 HTML。喜欢这个:
https://meteor.example.com/ => HTML page with no JavaScript
https://meteor.example.com/meteor/ => fully reactive Meteor experience
我知道我可以在/public
文件夹中放置一个纯索引.html页面,但是需要专门将其解决为https://meteor.example.com/index.html
。
我无法找到解决这种特殊情况的教程。
我找到了一个简单的解决方案,不需要对 Meteor 应用程序进行任何更改。我正在使用nginx来为流星网站提供服务。我让应用程序在本地端口上运行,并在nginx中设置代理规则:
server {
listen 80;
server_name example.com;
charset utf-8;
location ~ /(.+) {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
据我了解,这会将 url 请求传递给 Meteor 应用程序。如果public/
文件夹中有index.html
文件,则在请求http://example.com/
时将提供该文件而不是Meteor应用程序。
(如果我使用location /
而不是location ~ /(.+)
,就像我最初所做的那样,那么http://example.com/
请求将被视为对 Meteor 默认页面的请求。
部署
对于生产,我使用...
meteor build --server-only ../example-package
。我上传到服务器并解压缩。然后,我cd
进入bundle/programs/server
并以将运行该应用程序的用户身份运行npm install --production
。然后,我将文件夹添加为bundle/tmp/
和bundle/public/
,并将我的静态index.html
文件以及它所需的所有 CSS 和其他文件放在public/
文件夹中。
目前我正在使用export ROOT_URL=http://localhost && export PORT=3000 && node main.js
来启动该应用程序,但我计划在跳过所有正确的箍后立即使用Phusion乘客独立版。
下面是将 WebApp 与资产结合使用的示例 以下代码将进入服务器端文件,例如/server/main.js 在 meteor 项目中
WebApp.connectHandlers.use('/home', (req, res, next) => {
res.writeHead(200, {'Content-Type': 'text/html'});
const html = Assets.getText('home.html');
res.end(html);
});