Skip to content

Commit b866e3e

Browse files
committed
v2 Step 2: Setup Your server with Node, Express, and Babel
1 parent debefa3 commit b866e3e

File tree

4 files changed

+280
-17
lines changed

4 files changed

+280
-17
lines changed

config-overrides.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* config-overrides.js */
2+
const SWPrecacheWebpackPlugin = require("sw-precache-webpack-plugin");
3+
4+
module.exports = function override(config, env) {
5+
if (env === "production") {
6+
config.plugins = [
7+
...config.plugins.filter(
8+
plugin => plugin.constructor.name !== "SWPrecacheWebpackPlugin"
9+
),
10+
new SWPrecacheWebpackPlugin({
11+
dontCacheBustUrlsMatching: /\.\w{8}\./,
12+
filename: "service-worker.js",
13+
logger(message) {
14+
if (message.indexOf("Total precache size is") === 0) {
15+
return;
16+
}
17+
if (message.indexOf("Skipping static resource") === 0) {
18+
return;
19+
}
20+
console.log(message);
21+
},
22+
minify: true,
23+
staticFileGlobsIgnorePatterns: [/\.map$/, /asset-manifest\.json$/]
24+
})
25+
];
26+
}
27+
return config;
28+
};

package.json

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,44 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"babel-cli": "^6.26.0",
7+
"babel-core": "^6.26.3",
8+
"babel-preset-env": "^1.7.0",
9+
"compression": "^1.7.2",
10+
"express": "^4.16.3",
611
"react": "^16.4.1",
12+
"react-app-rewired": "^1.5.2",
713
"react-dom": "^16.4.1",
814
"react-scripts": "1.1.4"
915
},
1016
"scripts": {
11-
"start": "react-scripts start",
12-
"build": "react-scripts build",
17+
"start": "yarn dev",
18+
"build": "yarn build:client && yarn build:server",
19+
"build:client": "react-app-rewired build",
20+
"build:server": "babel src/server -d build/server",
21+
"dev": "concurrently \"yarn dev:server\" \"yarn dev:client\"",
22+
"dev:client": "react-scripts start",
23+
"dev:server": "DEV=true PORT=8081 babel-watch ./src/server/index.js",
24+
"now-start": "node build/server/index.js",
1325
"test": "react-scripts test --env=jsdom",
1426
"eject": "react-scripts eject"
27+
},
28+
"proxy": {
29+
"/api": {
30+
"target": "http://localhost:8081",
31+
"pathRewrite": {
32+
"^/proxy": ""
33+
},
34+
"changeOrigin": true
35+
}
36+
},
37+
"babel": {
38+
"presets": [
39+
"env"
40+
]
41+
},
42+
"devDependencies": {
43+
"babel-watch": "^2.0.7",
44+
"concurrently": "^3.6.0"
1545
}
16-
}
46+
}

src/server/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import compression from "compression";
2+
import express from "express";
3+
4+
const app = express();
5+
const port = process.env.PORT || 8080;
6+
app.use(compression());
7+
app.enable("trust proxy");
8+
9+
app.use((req, res, next) => {
10+
if (req.secure || req.headers.host === `localhost:${port}`) {
11+
next();
12+
} else {
13+
res.redirect(`https://${req.headers.host}${req.url}`);
14+
}
15+
});
16+
17+
app.use(express.static("./build"));
18+
19+
app.listen(port, err => {
20+
if (err) {
21+
console.log(err);
22+
return;
23+
}
24+
console.log(`App and API is live at http://localhost:${port}`);
25+
});

0 commit comments

Comments
 (0)