-
Install node.js. We recommend installing node v4.2.3 with n.
See their installation instructions and usage for more details, or just close your eyes and jump:
npm install -g n && n 4.2.3
-
Create a new npm project folder with some basic folders:
mkdir example-react-server && cd example-react-server
mkdir pages
-
Create a new npm project (follow the helpful npm prompts).
npm init
-
Install react-server and its related dependencies
npm install --save react-server express react
-
Create a page object:
touch pages/HelloWorld.js
And add the page class:
"use strict";
var React = require("react");
// TODO: jsx support
module.exports = class HelloWorldPage {
getElements() {
return React.createElement("div", null, "Hello, World!");
}
}
-
Create the routes:
touch routes.js
And add a HelloWorld route:
"use strict"
let HelloWorldPage = require("./pages/HelloWorld");
module.exports = {
// react-server middleware. TODO: add a "writing react-server middleware" example.
middleware: [],
routes: {
// This name isn't used for anything, it just needs to be unique.
Simple: {
// The relative path that this route responds to; so for an Express
// instance on 127.0.0.1 port 3000, respond to http://127.0.0.1/
path: ['/'],
// The http verb to respond to, e.g. get, put, post, patch, delete.
// This is optional, and defaults to 'get'.
method: 'get',
page: function () {
// TODO: returning an invalid object here (for instance, returning
// `(cb) => cb(HelloWorldPage)`) in a page makes it spin indefinitely
// but never throws an error.
return {
// TODO: Allow promises in addition to callbacks.
// i.e. return { promise: new promise((reject, resolve) => resolve(HelloWorldPage))}
done: function (cb) {
cb(HelloWorldPage);
}
};
}
},
}
}
-
Create the server:
touch server.js
And add the minimal server logic:
// "use strict" required to use `class` and `let`, but not strictly required
// for using react-server.
"use strict";
// Require dependencies
let rs = require("react-server"),
http = require("http"),
express = require("express"),
routes = require("./routes");
// TODO: Move this into rs.setConfig()
process.env.REACT_SERVER_CONFIGS = __dirname;
// By instantiating Express directly, you can use other Express middleware,
// or even multiple instances of react-server middleware.
let server = express();
// Register react-server as an Express middleware.
rs.middleware(server, routes);
// Use process.env.PORT if it's been set elsewhere, or 3000 if it hasn't.
// This allows you to set process.env.PORT to a different value in production
// without having to set it in development.
let port = process.env.PORT || 3000;
console.log("Start server...");
// Feature: we should go down on ^D as well as ^C
// Bug: (node) OutgoingMessage.flush is deprecated. Use flushHeaders instead.
// Start the server and listen on port.
let httpServer = http.createServer(server).listen(port, function () {
console.log("Started!");
});
console.log('Listening on port ' + port);
-
Start the server:
node server.js
-
Load the page in your browser, either by opening your favorite browser and
navigating to http://localhost:3000, or by opening it in your default browser from the command line:
open http://localhost:3000