From fa8298079c625f1904aebc714d7ee4df4b52cfd0 Mon Sep 17 00:00:00 2001 From: feychou Date: Sun, 8 Jan 2017 02:02:57 +0100 Subject: [PATCH 1/2] Add helmet as default middleware and change docs --- docs/guides/react-server-cli.md | 2 +- packages/react-server-cli/package.json | 1 + .../react-server-cli/src/commands/start.js | 2 + .../index.js | 48 +++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 packages/react-server-middleware-json-response/index.js diff --git a/docs/guides/react-server-cli.md b/docs/guides/react-server-cli.md index fb5cb0338..ef92d2668 100755 --- a/docs/guides/react-server-cli.md +++ b/docs/guides/react-server-cli.md @@ -143,7 +143,7 @@ export default (webpackConfig) => { In the `.reactserverrc` file add an option for `webpackConfig` that points to that function file and when React Server is setting up Webpack it will call your function with the result of the built in Webpack options, allowing you to make any modifications needed. This may also be specified on the command line with the `--webpack-config=` option. ### Use Custom Express Middleware -Currently the default Express Middlewares used are compression, body-parser, cookie-parser. If you need to setup custom express middleware you can do it with a setup function. +Currently the default Express Middlewares used are compression, body-parser, cookie-parser and helmet. If you need to setup custom express middleware you can do it with a setup function. ```javascript export default (server, reactServerMiddleware) => { diff --git a/packages/react-server-cli/package.json b/packages/react-server-cli/package.json index bc2fe8dd4..228558a79 100644 --- a/packages/react-server-cli/package.json +++ b/packages/react-server-cli/package.json @@ -27,6 +27,7 @@ "express": "^4.14.0", "extract-text-webpack-plugin": "~0.8.2", "file-loader": "~0.8.5", + "helmet": "^3.3.0", "json-loader": "^0.5.4", "less": "^2.7.1", "less-loader": "^2.2.3", diff --git a/packages/react-server-cli/src/commands/start.js b/packages/react-server-cli/src/commands/start.js index 8bdf9d60a..50e666802 100644 --- a/packages/react-server-cli/src/commands/start.js +++ b/packages/react-server-cli/src/commands/start.js @@ -4,6 +4,7 @@ import path from "path" import express from "express" import compression from "compression" import bodyParser from "body-parser" +import helmet from "helmet" import WebpackDevServer from "webpack-dev-server" import compileClient from "../compileClient" import handleCompilationErrors from "../handleCompilationErrors"; @@ -72,6 +73,7 @@ const startHtmlServer = (serverRoutes, port, bindIp, httpsOptions, customMiddlew server.use(compression()); server.use(bodyParser.urlencoded({ extended: false })); server.use(bodyParser.json()); + server.use(helmet()); rsMiddleware(); } diff --git a/packages/react-server-middleware-json-response/index.js b/packages/react-server-middleware-json-response/index.js new file mode 100644 index 000000000..23b180e58 --- /dev/null +++ b/packages/react-server-middleware-json-response/index.js @@ -0,0 +1,48 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _stringify = require('babel-runtime/core-js/json/stringify'); + +var _stringify2 = _interopRequireDefault(_stringify); + +var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); + +var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); + +var _createClass2 = require('babel-runtime/helpers/createClass'); + +var _createClass3 = _interopRequireDefault(_createClass2); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var JsonResponseMiddleware = function () { + function JsonResponseMiddleware() { + (0, _classCallCheck3.default)(this, JsonResponseMiddleware); + } + + (0, _createClass3.default)(JsonResponseMiddleware, [{ + key: 'setConfigValues', + value: function setConfigValues() { + return { isRawResponse: true }; + } + }, { + key: 'getContentType', + value: function getContentType() { + return 'application/json'; + } + }, { + key: 'getResponseData', + value: function getResponseData(next) { + return next().then(function (data) { + return (0, _stringify2.default)(data); + }); + } + }]); + return JsonResponseMiddleware; +}(); + +exports.default = JsonResponseMiddleware; +module.exports = exports['default']; From 0b13ab1f57ea64f02e2c2db24d27cbbc486c57ed Mon Sep 17 00:00:00 2001 From: feychou Date: Sun, 8 Jan 2017 14:32:47 +0100 Subject: [PATCH 2/2] Update docs --- docs/guides/react-server-cli.md | 2 +- .../index.js | 48 ------------------- 2 files changed, 1 insertion(+), 49 deletions(-) delete mode 100644 packages/react-server-middleware-json-response/index.js diff --git a/docs/guides/react-server-cli.md b/docs/guides/react-server-cli.md index ef92d2668..f2e0a7b6f 100755 --- a/docs/guides/react-server-cli.md +++ b/docs/guides/react-server-cli.md @@ -143,7 +143,7 @@ export default (webpackConfig) => { In the `.reactserverrc` file add an option for `webpackConfig` that points to that function file and when React Server is setting up Webpack it will call your function with the result of the built in Webpack options, allowing you to make any modifications needed. This may also be specified on the command line with the `--webpack-config=` option. ### Use Custom Express Middleware -Currently the default Express Middlewares used are compression, body-parser, cookie-parser and helmet. If you need to setup custom express middleware you can do it with a setup function. +Currently the default Express Middlewares used are compression, body-parser, cookie-parser, helmet with default configuration. If you need to setup custom express middleware you can do it with a setup function. ```javascript export default (server, reactServerMiddleware) => { diff --git a/packages/react-server-middleware-json-response/index.js b/packages/react-server-middleware-json-response/index.js deleted file mode 100644 index 23b180e58..000000000 --- a/packages/react-server-middleware-json-response/index.js +++ /dev/null @@ -1,48 +0,0 @@ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _stringify = require('babel-runtime/core-js/json/stringify'); - -var _stringify2 = _interopRequireDefault(_stringify); - -var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); - -var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); - -var _createClass2 = require('babel-runtime/helpers/createClass'); - -var _createClass3 = _interopRequireDefault(_createClass2); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -var JsonResponseMiddleware = function () { - function JsonResponseMiddleware() { - (0, _classCallCheck3.default)(this, JsonResponseMiddleware); - } - - (0, _createClass3.default)(JsonResponseMiddleware, [{ - key: 'setConfigValues', - value: function setConfigValues() { - return { isRawResponse: true }; - } - }, { - key: 'getContentType', - value: function getContentType() { - return 'application/json'; - } - }, { - key: 'getResponseData', - value: function getResponseData(next) { - return next().then(function (data) { - return (0, _stringify2.default)(data); - }); - } - }]); - return JsonResponseMiddleware; -}(); - -exports.default = JsonResponseMiddleware; -module.exports = exports['default'];