Chapter 4. Customize the Cloud App

To better understand how the Cloud App works, make a minor modification to the code. Add a timestamp field with the value of the current UNIX time stamp to the server response. In the next section, you will modify the Client App to display the time stamp.

  1. Navigate to the Projects area using the navigation bar at the top.
  2. Open the My Hello World Project project.
  3. Open the Cloud App.
  4. Click Editor on the sidebar on the left.

    This area lets you edit the source code of any file in the Git repository of the Cloud App. The Cloud App in this project is a Node.js web application framework called Express.

    Editor

    Note

    The Studio editor provides a quick and easy way to edit files, however, Red Hat recommends cloning the code and using a local editor or IDE to develop applications.

  5. Open the application.js file.

    application.js handles all requests to the Cloud App. The Client App sends requests to the /hello endpoint and the application.js file routes those requests to another file called hello.js.

    app.use('/hello', require('./lib/hello.js')());

    To learn more about routing in Express, take a look at the Express Router documentation.

Change the lib/hello.js file to return a timestamp in the response.

  1. Open lib/hello.js.
  2. Add a timestamp property to the POST response object, with the value of the current UNIX time stamp.

    Find this line in the POST handler:

    res.json({msg: 'Hello ' + world});

    Change that line to the following:

    res.json({msg: 'Hello ' + world, timestamp: new Date().getTime() });

    The POST handler now looks like this:

    hello.post('/', function(req, res) {
      console.log(new Date(), 'In hello route POST / req.body=', req.body);
      var world = req.body && req.body.hello ? req.body.hello : 'World';
    
      // see http://expressjs.com/4x/api.html#res.json
      res.json({msg: 'Hello ' + world, timestamp: new Date().getTime() });
    });
  3. Save your changes by clicking File > Save in the editor.

    Editor

    The changes are saved to the Git repository of the Cloud App. To propagate the changes to the running instance, you must re-deploy the Cloud App.

  4. Click Deploy on the sidebar on the left.
  5. Click Deploy Cloud App.