Code Monkey home page Code Monkey logo

isomorphic-tutorial's Introduction

Isomorphic JavaScript Tutorial

This is a small sample app built to demonstrate isomorphic JavaScript concepts.

Overview

Here we've built the minimum-viable example of an isomorphic JavaScript app: an app that can run on both client and server. Check out my blog post on isomorphic JavaScript for some background.

This is a simple Express "blog" app that lists blog posts. Each page is fully rendered on the server, however upon subsequent navigation, we use the HTML5 History API, aka pushState, to fetch the data for that page from the API and render the HTML client-side using React.

Under the hood

We combine a few modules together to build an isomorphic JavaScript app. Each of these modules was built to support both the client and the server, and by creating some small shims around them, we can abstract out the differences to create the same API for both client and server.

We use the following modules on both client and server:

On top of a basic Express app.

We use Browserify and Grunt to package our server-side CommonJS modules in a way that allows us to use them in the client-side.

Getting it running

Install Node.js >= 0.10.x

If Node.js version >= 0.10.x is not already installed on your system, install it so you can run this app.

Check if it's installed

The command which node will return a path to your installed version of Node.js, if it exists on your system.

$ which node
/usr/local/bin/node

If it is installed, make sure it's >= 0.10.x.

$ node --version
v0.10.33

To install

Mac

Preferably install using Homebrew:

$ brew install node
Else

Otherwise, go to the nodejs.org and download the binary to install on your system.

Install grunt-cli

This app uses Grunt to build its assets. To run Grunt, we need to install the grunt-cli package globally on your system using NPM.

$ npm install -g grunt-cli

Clone this repo onto your machine

$ cd ~/code
$ git clone [email protected]:spikebrehm/isomorphic-tutorial.git
$ cd isomorphic-tutorial

Run npm install to install dependenices

$ npm install
npm http GET https://registry.npmjs.org/superagent
npm http GET https://registry.npmjs.org/handlebars
npm http GET https://registry.npmjs.org/director
...

Run the app!

We'll start up our local Node.js web server using Grunt, so it can automatically do useful things for us when we change files like recompile our assets and restart the server.

$ grunt server

This will start our local web server on port 3030.

You can view it in your web browser at http://localhost:3030/

Adding features

Now that you've got it running, you can start adding some features to get to know the problem space better.

We've got some branches you can check out that show how to add certain features.

Adding the Moment library for date formatting

compare to master

This branch adds the Moment date formatting library for use on the /posts/:id page.

Use Marked library to add Markdown to posts

compare to master

This branch adds the Marked library to allow formatting of blog post bodies in Markdown, in just a few lines of code.

Create a route for adding posts

compare to master

Create a page with a form at /posts/new, which POSTs to the /api/posts.json endpoint.

License

MIT

isomorphic-tutorial's People

Contributors

couchand avatar spikebrehm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.