UNPKG

2.3 kBJavaScriptView Raw
1"use strict";
2// tslint:disable:no-console
3var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const index_1 = require("../index");
13const manager = new index_1.PluginManager();
14function run() {
15 return __awaiter(this, void 0, void 0, function* () {
16 console.log("Installing express...");
17 yield manager.install("express", "4.16.2");
18 console.log("Installing react...");
19 yield manager.install("react", "16.0.0");
20 console.log("Installing react-dom...");
21 yield manager.install("react-dom", "16.0.0");
22 const express = manager.require("express");
23 const React = manager.require("react");
24 const ReactDOMServer = manager.require("react-dom/server");
25 const app = express();
26 app.get("/", function (req, res) {
27 class Hello extends React.Component {
28 render() {
29 return React.createElement("div", null, `Hello ${this.props.toWhat} from React!`);
30 }
31 }
32 const elementToRender = React.createElement(Hello, { toWhat: "World" }, null);
33 const reactResult = ReactDOMServer.renderToString(elementToRender);
34 res.send(reactResult);
35 });
36 const server = app.listen(3000, function () {
37 console.log("Example app listening on port 3000, closing after 20 secs.!");
38 });
39 setTimeout(() => __awaiter(this, void 0, void 0, function* () {
40 server.close();
41 console.log("Uninstalling plugins...");
42 yield manager.uninstallAll();
43 }), 20000);
44 });
45}
46run()
47 .catch(console.error.bind(console));
48//# sourceMappingURL=express-react.js.map
\No newline at end of file