Along with aiming to use the bleeding-edge of JavaScript (within reason- and all thanks to Babel), this repository represents a choice of frameworks and libraries I think work well together for an enjoyable NodeJS and frontend coding experience.
A short rundown of the various technologies:
| Folder | What’s in it |
| /client
| /react
App
component and whatever else makes up the layout./svg
- SVG React components. Placed directly in the code as if they were normal functional React components. Much easier to incorporate into layouts. They’re usually very small in size (1-3kb) so it’s not a data hog and means they load instantly with SSR too./routes
- The React-Router defined routes for our app/styles
.scss
, Sass style modules. The main one, entry.scss
, is included in our entry.js
so webpack knows it’s part of our client bundle (and hot reloading works because it becomes part of the “hot” bundle)|
| /crossover
| /api
isomorphic-fetch
/constants
* Pretty self explanatory, just constants to be used throughout the app. For now it’s just different types of tabs. This allows us to think about certain parts of the UI as one, constant thing and therefore we can create things like language dictionaries that can be access according to a language constant and the UI constant but the components remain exactly the same.
/mobx
/stores
- All the MobX stores in seperate modules, which are basically just classes with @decorators
to keep things neat. some of them access API files from above.allStores.js
- The only part of the code that feels too static too me, but required for the next step. We just bundle all the modules from the folder above into a single exported object. This enables what happens in the next file.store-utils.js
- Creating and hydrating stores (for now the only utils). It requires that we remember to put our stores in the imported allStores.js
above so they are included in the processes. Used on the server side first to create new stores (which are then used normally before the request returns), and then hydrated on the client side (taking the state that came from the server and mirroring it exactly on the client side MobX stores).entry.js
baseReact()
function which returns our main React app (components) that we want to mount onto our page. This is so there is a single source of truth for both server and client. It is also the entry file for our client javascript, so it takes control of getting the MobX state from the server-generated page and injecting that into our client MobX stores before rendering the exact same React components the server just spat out. But it is not actually rendered again, because React is smart, React simply sees it’s all the same so doesn’t re-render the dom but simply makes what’s already there dynamic and functional.|
| /images
| Self explanatory |
| /server
| babel-server.js
babel-register
which insures that all further files required by our server will be transpiled to ES5 javascript code (understood by Node).server.js
/middleware
folder and a router from /router
/middleware/crossoverMiddleware.js
to see how MobX state is first created, and then used to render our React at the end of a request.async
functions and using the await
key word for request/response streams. Makes things much easier to reason about, especially with our MobX stores also returning async / await
functions which themselves can be await
’ed. Take a look inside the router file to see it in action./static
directory thanks to some middleware which you can find in the /middleware/basicMiddleware.js
file.|
npm install
Or, even better (if you are using yarn
):
yarn install
To start the development server (which is what you want most of the time)
npm run dev
To start the production server is the usual npm start
, but the code has to be built first, so:
npm run build
npm start
Or for quick production testing npm run build-start
Before deployment to a production server, your code should always be built first- so that the default npm start
can be used to spin up quickly.
I’m using the babel-register
module which is required before any other server code and automatically converts any further requires / imports to ES5 javascript. This is for development purposes only (npm run dev
) - as recommended.
The server code (and the React code which is used on the server) is then built into the /built
folder for production use ( npm start
)
Author: lostpebble
Source Code: https://github.com/lostpebble/koa-mobx-react-starter
#react #reactjs #javascript