Learn to scaffold out a complete web application that uses a Kitura server to serve a React-based UI. Using the recently released Bluemix Developer Console and the Developer CLI you can have something scaffolded out in less then 30 seconds.

Prerequisites:

Zero to Hero

  1. Use the dev plugin to create a new project

     $ bx dev create
    
  2. Select Web pattern, Webpack + React, and Swift

    Scaffold-Step1

  3. Name your project, give it a hostname (needs to be unique), and select no services

    Scaffold-Step2

  4. Wait for your project to be generated (should take about 30 seconds)

    Scaffold-Step3

    Once completed, you should see “The project, … has been saved into the current directory.”

  5. Change into the directory of your project

     cd MyReact
    
  6. Install the node packages and run gulp:

     $ npm install
    

    Install node dependencies

    Run gulp

  7. Build the Swift project (takes 2 minutes)

     $ swift build -Xlinker -lc++
    

    When you run this command, the Swift Package Manager will download all of the dependencies your project needs into your Packages directory. Downloading the packages on your first compile, can take a bit of time. Future builds will be very fast, however.

    Build Kitura app

  8. Run the application

    It’s time to run your application.

    $ .build/debug/MyReact
    

    You will probably be getting a message about accepting incoming connections if you’re on macOS. Select ‘Allow’. Notice that at the end of the run, the logger says that the server is running on port 8080.

    Accept incoming

  9. Open your browser and see your running application.

    Server running

Modifying your application

  1. Open your project in your favorite editor or IDE

    • If you want to use Xcode, run “swift package generate-xcodeproj” first.
    • VSCode, Atom, and Sublime are all good choices, too.

    View code

  2. Make changes, for instance:

     import React from 'react';
     import {render} from 'react-dom';
    
     // load stylesheet
     require("../sass/default.scss");
    
     class App extends React.Component {
         render () {
             return (
                 <section>
                     <p>Hello, World!</p>
                 </section>
             );
         }
     }
    
     render(<App/>, document.getElementById('app'));
    
  3. Re-run

     npm install
    
  4. Refresh your browser

    View code