Skip to content

Commit

Permalink
allowing custom middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrip committed Aug 23, 2018
1 parent 3ffdf20 commit bd19d42
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
12 changes: 9 additions & 3 deletions packages/startd-server/lib/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require("react");

var _react2 = _interopRequireDefault(_react);
Expand Down Expand Up @@ -28,20 +30,24 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

var App = require(APP_PATH).default;

var AppModule = require(APP_PATH);
var App = AppModule.default;
var app = new _koa2.default();

app.use((0, _koaStatic2.default)(_path2.default.resolve(process.cwd(), "public"), { maxage: 0 }));

if (AppModule.hasOwnProperty("middleware")) {
app.use(AppModule.middleware);
}

app.use(function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(ctx) {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
ctx.type = "html";
ctx.body = "<!doctype html><html lang=\"en\">\n <head>\n <meta charset=\"utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <script type=\"text/javascript\" src=\"" + BUNDLE_PATH + "\"></script>\n </head>\n <body>\n <div id=\"root\">" + (0, _server.renderToString)(_react2.default.createElement(App, null)) + "</div>\n </body>\n </html>";
ctx.body = "<!doctype html><html lang=\"en\">\n <head>\n <meta charset=\"utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <script type=\"text/javascript\" src=\"" + BUNDLE_PATH + "\"></script>\n </head>\n <body>\n <div id=\"root\">" + (0, _server.renderToString)(_react2.default.createElement(App, { ctx: _extends({}, ctx) })) + "</div>\n </body>\n </html>";

case 2:
case "end":
Expand Down
10 changes: 7 additions & 3 deletions packages/startd-server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import serve from "koa-static";
import path from "path";
import { renderToString } from "react-dom/server";

const App = require(APP_PATH).default;

const AppModule = require(APP_PATH);
const App = AppModule.default;
const app = new Koa();

app.use(serve(path.resolve(process.cwd(), "public"), { maxage: 0 }));

if (AppModule.hasOwnProperty("middleware")) {
app.use(AppModule.middleware);
}

app.use(async ctx => {
ctx.type = "html";
ctx.body = `<!doctype html><html lang="en">
Expand All @@ -21,7 +25,7 @@ app.use(async ctx => {
<script type="text/javascript" src="${BUNDLE_PATH}"></script>
</head>
<body>
<div id="root">${renderToString(<App />)}</div>
<div id="root">${renderToString(<App ctx={{ ...ctx }} />)}</div>
</body>
</html>`;
});
Expand Down

0 comments on commit bd19d42

Please sign in to comment.