Cosmic JS Blog Stay tuned for community news, company announcements and updates from the Cosmic JS team.

How to Build a Cross-Platform Blog Using React Native and Node.js


by Tony Spiro on October 4, 2016

We live in a world of inter-connected devices, each powered by their own software, built with their own circuits and protected by their own casing.  The best way to deliver information to these seemingly closed-off systems is with an API.  Cosmic JS is an API-first cloud-based content platform that makes it not only easy to add dynamic content to any existing or new website, but makes it possible to seamlessly deliver content across devices and platforms.

In this article I’m going to show you how to quickly and easily create a cross-platform blog that will connect your content across Web, iOS and Android.  With content powered by Cosmic JS, you will be able to edit your content once and deliver the updates simultaneously across all devices and platforms.  Let’s get started.

TL;DR

Check out the demo.


Website


To get started, download the Cross-Platform Blog on GitHub which is based on the Node.js Website Boilerplate.  This comes with everything you need to get started with a basic website as well as the codebase for the native applications running React Native for iOS and Android.

git clone https://github.com/cosmicjs/cross-platform-blog
cd cross-platform-blog/web
npm install
npm run development

The root of our application runs at app-server.js.  Looking at this file, you'll see how routes are set up and how views are rendered to the routes.

Now that we have our website running, let’s open up another terminal window and get our iOS and Android apps built.

React Native Bridges the Gap

React Native is a very powerful way to build applications for both iOS and Android.  Because most, if not all, of the codebase can be shared between the two platforms, by using React Native you can build your apps in half the time.  If you haven’t gotten your local environment setup for React Native development, go to the React Native website and follow the steps.  Honestly I found setting up the iOS development environment to be super easy, but setting up the Android, not so much.  Mainly because I had to search more to figure it all out.  In short, you need to download Android Studio and make sure you have the correct version of the Java SDK installed and make sure you have all of the developer tools installed.

iOS

To install and run the iOS React Native application run the following commands:

cd cross-platform-blog/ios
npm install
react-native run-ios


Android

Run the following commands to run the Android application (make sure you have your Android device emulator open):

cd cross-platform-blog/android
npm install
react-native android

Looking at index.ios.js and index.android.js you will notice that the root of our application for both iOS and Android runs at App.js.   And all other components LandingPage.js, BlogList.js and BlogPost.js are shared between the two platforms as well.

When we run our react-native commands, our React Native code base is translated to both Objective-C and Java languages.  It is not a “hybrid app” or web-based at all.  It basically just translates your JavaScript (React) building blocks together into the native language of each platform.

Connect to the CMS API

If you don't have an account yet, sign up for Cosmic JS to set up your bucket.  Your bucket is the place to add and edit content for your cross-platform blog.  To upload the demo content from the Cross-Platform Blog on GitHub, go to Your Bucket > Import / Export.  Then upload the bucket.json file located in the root folder of the Cross-Platform Blog repository.  This will upload all of the demo data and files from the Cross-Platform Blog demo application.   To configure your local application to point to your bucket, simply edit your config files in both web and native folders to point to "your-bucket-slug".

// config.js
export default {
  COSMIC_BUCKET: 'your-bucket-slug'
}

Now when you edit content in your Cosmic JS bucket, you will see that the content automatically updates across Web, iOS and Android applications.  By connecting your applications to Cosmic JS, you are using a powerful tool that breaks down the barriers between devices and programming languages.

I hope you found this tutorial helpful.  If you have any questions about how Cosmic JS can help your team build websites and applications faster and easier, reach out to us on Twitter and join our community on Slack.

You may also like


At the request of the Cosmic JS Community, new Multi-Bucket Pricing Tiers are now Available for teams. Introducing, Cosmic Clusters. 

In this article I’m going to show you how to get the best of both worlds:  A website that is both dynamic, has content powered by the Cosmic JS API that is also a static website.  Our static website will be easily managed by a content editor using the Cosmic JS CMS and the developer will also be able to use Markdown files.

Variety is the spice of life and it is essential to flexible content management.  Currently I'm seeing the trend of data storage move from relational to on-the-fly structures.  Relational databases like MSSQL and MySQL are being less favored and more developers are choosing to move to NoSQL databases like MongoDB.  By more developers I include myself because this is a very flexible and intuitive way to store data.  Key / Value, or as I like to call on-the-fly, content storage is the next stage of content management.  Relational structures bind where on-the-fly sets free.  This is the theory behind Cosmic JS metafields.

Content items are like people in that they come in all shapes, sizes and colors.  Cosmic JS objects allow you to create metafields which give you the power to add on-the-fly key / value pairs.  Create infinite metafields and drag and drop them into parent / child relationships. 

I find it so much easier to keep track of my key / value pairs per object rather than managing a relational schema.  I believe this is the new way of on-the-fly content management that will allow for flexibility and scalability of future content.

The same API that delivers your content to all web-connected devices can also store the media you need to publish.

Cosmic JS is on a mission to help you build apps faster.  And with that goal in mind, we're happy to announce some additions to our API that will help you manage your application content faster and easier, right from the Cosmic JS API.

In this installment of the Cosmic JS Developer Spotlight Series, we sat down with Jamie Introcaso, an experienced Software Developer residing in Charlotte, North Carolina. Jamie is an active member of the Cosmic JS Community and recently wrote a blog detailing his migration from WordPress to Cosmic JS. With more community projects on the way, we're excited to interview one of our own for this Spotlight. 😎 Follow Jamie on his Cosmic-powered website, Twitter and LinkedIn and enjoy the conversation.