Skip to content

Commit ec15ada

Browse files
committed
updates render middlewares and scripts
1 parent 3df3090 commit ec15ada

File tree

5 files changed

+32
-19
lines changed

5 files changed

+32
-19
lines changed

config/paths.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
66

77
module.exports = {
88
serverBuild: resolveApp("build-server"),
9-
serverIndexJs: resolveApp("src/server.js")
9+
serverIndexJs: resolveApp("src/server.js"),
10+
customScriptConfig: resolveApp(".react-scripts-ssr.json")
1011
};

scripts/proxy.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const httpProxy = require("http-proxy");
22
const http = require("http");
33
const fs = require("fs");
44
const program = require("commander");
5-
const pathCustomConfig = "./.react-scripts-ssr.json";
65
const openBrowser = require("react-dev-utils/openBrowser");
6+
const pathsSSRScripts = require("../config/paths");
77

88
program
99
.version("0.1.0")
@@ -31,10 +31,14 @@ program
3131

3232
let customHttpProxyConfig = { proxy: {} };
3333
try {
34-
if (fs.existsSync(pathCustomConfig)) {
35-
customConfig = JSON.parse(fs.readFileSync(pathCustomConfig));
34+
if (fs.existsSync(pathsSSRScripts.customScriptConfig)) {
35+
customConfig = JSON.parse(
36+
fs.readFileSync(pathsSSRScripts.customScriptConfig)
37+
);
3638
if (customConfig.proxy) {
37-
customHttpProxyConfig = customConfig;
39+
customHttpProxyConfig = customConfig.proxy;
40+
} else {
41+
throw new Error("customConfig doesn't have a key called proxy");
3842
}
3943
}
4044
} catch (error) {
@@ -44,22 +48,18 @@ try {
4448
}
4549

4650
const WEB_HOST =
47-
program.webHost || customHttpProxyConfig.proxy.webHost || "localhost";
48-
const WEB_PORT =
49-
program.webPort || customHttpProxyConfig.proxy.webPort || "3000";
51+
program.webHost || customHttpProxyConfig.webHost || "localhost";
52+
const WEB_PORT = program.webPort || customHttpProxyConfig.webPort || "3000";
5053
const WEB_URL = `http://${WEB_HOST}:${WEB_PORT}`;
5154
const API_URL =
52-
program.apiUrl ||
53-
customHttpProxyConfig.proxy.apiUrl ||
54-
"http://localhost:8080";
55-
const PROXY_PORT =
56-
program.proxyPort || customHttpProxyConfig.proxy.proxyPort || 5050;
55+
program.apiUrl || customHttpProxyConfig.apiUrl || "http://localhost:8080";
56+
const PROXY_PORT = program.proxyPort || customHttpProxyConfig.proxyPort || 5050;
5757
const PROXY_HOST =
58-
program.proxyHost || customHttpProxyConfig.proxy.proxyHost || "localhost";
58+
program.proxyHost || customHttpProxyConfig.proxyHost || "localhost";
5959

6060
const proxy = httpProxy.createProxyServer({
6161
ws: true,
62-
...customHttpProxyConfig.proxy
62+
...customHttpProxyConfig
6363
});
6464

6565
var server = http.createServer((req, res) => {

scripts/start.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ choosePort(HOST, DEFAULT_PORT)
6868
// We have not found a port.
6969
return;
7070
}
71+
72+
process.env.REACT_APP_CLIENT_PORT = port;
7173
const protocol = process.env.HTTPS === "true" ? "https" : "http";
7274
const appName = require(paths.appPackageJson).name;
7375
const urls = prepareUrls(protocol, HOST, port);
@@ -120,15 +122,25 @@ choosePort(HOST, DEFAULT_PORT)
120122

121123
if (!isServerRunning) {
122124
isServerRunning = true;
123-
124125
const nodemon = exec(
125-
"npx nodemon --watch build-server/index.js build-server/index.js"
126+
"npx nodemon --watch build-server/index.js build-server/index.js",
127+
(err, stdout, stderr) => {
128+
if (err) {
129+
console.error(`exec error: ${err}`);
130+
return;
131+
}
132+
133+
console.log(`Number of files ${stdout}`);
134+
}
126135
);
127136

128137
// This is to outpout in the terminal the child process
129138
nodemon.stdout.on("data", data => {
130139
console.log(data.toString());
131140
});
141+
nodemon.stderr.on("data", data => {
142+
console.error(data);
143+
});
132144
nodemon.on("exit", code => {
133145
console.log(
134146
`nodemon process exited with code ${code.toString()}`

src/middlewares.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function createSSRMiddleware(middleware) {
1717
);
1818
} else {
1919
router.use(
20-
["/static", "/sockjs-node"],
20+
["/static", "/sockjs-node", "/api"],
2121
proxy({
2222
target: `http://localhost:${process.env.REACT_APP_DEV_SERVER_PORT}`,
2323
ws: true

src/render.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async function render(pageData, req, res) {
2323
if (process.env.NODE_ENV === "development") {
2424
const fetch = require("node-fetch").default;
2525
const pageDevResponse = await fetch(
26-
`http://localhost:${process.env.REACT_APP_DEV_SERVER_PORT}`
26+
`http://localhost:${process.env.REACT_APP_DEV_SERVER_PORT}${req.path}`
2727
);
2828
const pageTemplate = await pageDevResponse.text();
2929
const page = injectHTML(pageTemplate, pageData);

0 commit comments

Comments
 (0)