静态脚本文件覆盖 GET 方法服务器端渲染
Static Script file overrides the GET method Server-side Rendering
存储库- https://github.com/sapinder-pal/Rest-Countries-App-ReactJS-SASS
我正在尝试将我的 React 应用程序转换为 SSR 系统。虽然它可以在 localhost 上运行,但在部署到 Heroku 时会抛出应用程序错误。构建日志显示 成功 ,但在访问 url 时抛出 应用程序错误 .
请参阅 EDIT 1,我在其中说明了根本错误。
应用程序日志-
2020-09-21T05:23:05.915898+00:00 app[web.1]: npm ERR! rest-countriers@1.0.0 start: `node server/index.js`
2020-09-21T05:23:05.916046+00:00 app[web.1]: npm ERR! Exit status 1
2020-09-21T05:23:05.916211+00:00 app[web.1]: npm ERR!
2020-09-21T05:23:05.916335+00:00 app[web.1]: npm ERR! Failed at the rest-countriers@1.0.0 start script.
2020-09-21T05:23:05.916489+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-09-21T05:23:06.005989+00:00 app[web.1]:
2020-09-21T05:23:06.006055+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-09-21T05:23:06.006056+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-09-21T05_23_05_918Z-debug.log
2020-09-21T05:23:06.234287+00:00 heroku[web.1]: Process exited with status 1
2020-09-21T05:23:06.275353+00:00 heroku[web.1]: State changed from starting to crashed
server/index.js-
require('ignore-styles');
require('@babel/register')({
ignore: [/(node_module)/],
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: ['@babel/transform-runtime']
});
require('./server');
server/server.js-
import express from 'express';
import fs from 'fs';
import path from 'path';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import {StaticRouter} from 'react-router-dom';
import App from '../src/App';
const PORT = process.env.PORT || 8080;
const app = express();
app.use(express.static(path.resolve('./build')))
app.get('*', (req, res) => {
fs.readFile(path.resolve('./build/index.html'), 'utf-8', (err, data) => {
if (err) {
console.log(err);
return res.status(500).send('Some error happened');
}
const context = {};
const app = ReactDOMServer.renderToString(
<StaticRouter location={req.url} context={context}>
<App />
</StaticRouter>
)
return res.send(
data.replace(
'<div id="root"></div>',
`<div id="root">${app}</div>`
)
);
});
});
app.listen(PORT, () => {
console.log(`App launched on ${PORT}`);
});
编辑 1-
看来我找到了根本错误,但不太清楚原因-
/**** app.get() WORKS ONLY IF I DON'T SERVER THE 'BUILD' FOLDER. ****/
app.use(express.static(path.resolve('./build')));
/**** This function doesn't log anything if the *build* folder is served.
I got assured when I visited **Network** tab in devtools where I saw that
the *initial preview* for html file is blank, which means the file is not
rendered server-side ****/
app.get('*', (req, res) => {
console.log('got request');
....
}
我有index.html、index.css和index.bundle.js 在 build 文件夹中。
您需要在 package.json 中添加 heroku-postbuild 命令
您的命令类似于
scripts:{
...
"build": ***,
"heroku-postbuild": "npm install && npm run build",
}
这将 运行 并安装您的依赖项,并将 运行 生成命令。
尝试删除 'return' 关键字
res.send(
data.replace(
<div id="root"></div>',
<div id="root">${app}</div>`
)
)
端口应该是
process.env.PORT || 3000
就是这样。它现在应该可以工作了。
已解决!!!
代码有两个问题-
- 第一个问题是
app.get()
方法没有 运行 已通过将 app.use()
语句移动到前一个方法下方解决。
不过,我还是不太明白这是什么原因,因为我觉得一个中间件还是会运行 before 路由方法中的回调函数。如果你知道这里发生了什么,请通过评论告诉我!
- 导致
start
脚本失败的第二个问题是因为我使用 @babel/core
作为 dev-dependency。所以我 re-installed 它仅作为 dependency,并修复了错误。
如果有人能向我解释第一点,我将不胜感激!
存储库- https://github.com/sapinder-pal/Rest-Countries-App-ReactJS-SASS
我正在尝试将我的 React 应用程序转换为 SSR 系统。虽然它可以在 localhost 上运行,但在部署到 Heroku 时会抛出应用程序错误。构建日志显示 成功 ,但在访问 url 时抛出 应用程序错误 .
请参阅 EDIT 1,我在其中说明了根本错误。
应用程序日志-
2020-09-21T05:23:05.915898+00:00 app[web.1]: npm ERR! rest-countriers@1.0.0 start: `node server/index.js`
2020-09-21T05:23:05.916046+00:00 app[web.1]: npm ERR! Exit status 1
2020-09-21T05:23:05.916211+00:00 app[web.1]: npm ERR!
2020-09-21T05:23:05.916335+00:00 app[web.1]: npm ERR! Failed at the rest-countriers@1.0.0 start script.
2020-09-21T05:23:05.916489+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-09-21T05:23:06.005989+00:00 app[web.1]:
2020-09-21T05:23:06.006055+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-09-21T05:23:06.006056+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-09-21T05_23_05_918Z-debug.log
2020-09-21T05:23:06.234287+00:00 heroku[web.1]: Process exited with status 1
2020-09-21T05:23:06.275353+00:00 heroku[web.1]: State changed from starting to crashed
server/index.js-
require('ignore-styles');
require('@babel/register')({
ignore: [/(node_module)/],
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: ['@babel/transform-runtime']
});
require('./server');
server/server.js-
import express from 'express';
import fs from 'fs';
import path from 'path';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import {StaticRouter} from 'react-router-dom';
import App from '../src/App';
const PORT = process.env.PORT || 8080;
const app = express();
app.use(express.static(path.resolve('./build')))
app.get('*', (req, res) => {
fs.readFile(path.resolve('./build/index.html'), 'utf-8', (err, data) => {
if (err) {
console.log(err);
return res.status(500).send('Some error happened');
}
const context = {};
const app = ReactDOMServer.renderToString(
<StaticRouter location={req.url} context={context}>
<App />
</StaticRouter>
)
return res.send(
data.replace(
'<div id="root"></div>',
`<div id="root">${app}</div>`
)
);
});
});
app.listen(PORT, () => {
console.log(`App launched on ${PORT}`);
});
编辑 1-
看来我找到了根本错误,但不太清楚原因-
/**** app.get() WORKS ONLY IF I DON'T SERVER THE 'BUILD' FOLDER. ****/
app.use(express.static(path.resolve('./build')));
/**** This function doesn't log anything if the *build* folder is served.
I got assured when I visited **Network** tab in devtools where I saw that
the *initial preview* for html file is blank, which means the file is not
rendered server-side ****/
app.get('*', (req, res) => {
console.log('got request');
....
}
我有index.html、index.css和index.bundle.js 在 build 文件夹中。
您需要在 package.json 中添加 heroku-postbuild 命令 您的命令类似于
scripts:{
...
"build": ***,
"heroku-postbuild": "npm install && npm run build",
}
这将 运行 并安装您的依赖项,并将 运行 生成命令。
尝试删除 'return' 关键字
res.send(
data.replace(
<div id="root"></div>',
<div id="root">${app}</div>`
)
)
端口应该是 process.env.PORT || 3000
就是这样。它现在应该可以工作了。
已解决!!!
代码有两个问题-
- 第一个问题是
app.get()
方法没有 运行 已通过将app.use()
语句移动到前一个方法下方解决。
不过,我还是不太明白这是什么原因,因为我觉得一个中间件还是会运行 before 路由方法中的回调函数。如果你知道这里发生了什么,请通过评论告诉我!
- 导致
start
脚本失败的第二个问题是因为我使用@babel/core
作为 dev-dependency。所以我 re-installed 它仅作为 dependency,并修复了错误。
如果有人能向我解释第一点,我将不胜感激!