Oracle by Example brandingCreating a Node.js RESTful Application in Oracle Application Container Cloud Service

part 0Before You Begin

This 10-minute tutorial shows you how to develop a simple message board application. Using REST calls, you can read existing comments and create new comments on the message board.

Background

Oracle Application Container Cloud Service provides a lightweight infrastructure that lets you deploy Java Platform, Standard Edition (SE), PHP, and Node.js applications to Oracle Cloud. You can use Node.js cloud service in Oracle Application Container Cloud Service to develop RESTful web services, and then integrate them with your client applications.

What Do You Need?


part 1Develop a Sample RESTful Node.js Service

  1. Create a JavaScript file and name it server.js.
  2. Open server.js in an editor and create a simple Node.js server.
    var http = require('http');
    var PORT = 8089; 
    var server = http.createServer(function (request, response) {
        response.end("LATER ON, YOU WILL PLACE CODE HERE");
    });
    
    server.listen(PORT, function () {
        console.log('Server running...');
    });

    This code fragment creates a service that listens on HTTP port 8089. In a later step, you will change this port with an Oracle Application Container Cloud Service variable.

  3. Test your server.
    node server.js
  4. In a browser window, go to http://localhost:8089 and look for the following message: "LATER ON, YOU WILL PLACE CODE HERE."
  5. To stop the server, press CTRL+C.
  6. Add the following variable declarations after the var PORT declaration:
    var topicList = [];
    var topicDetail = {};
    var currentId = 123;
  7. Add the following functions after the variable declarations:
    function addTopic(tTitle, tText) {
       console.log("addTopic(" + tTitle + "," + tText + ")");
       var topicId = ++currentId;
       topicList.push({title: tTitle, id: topicId});
       topicDetail[topicId] = {title: tTitle, text: tText, comments: []};
       return topicId;
    }
    
    function addComment(topicId, text) {
       console.log("addComment(" + topicId + "," + text + ")");
       topicDetail[topicId].comments.push(text);
    }                      
  8. Create sample messages.
    var id1 = addTopic("Topic 1","Topic 1 content");
    var id2 = addTopic("Topic 2","Topic 2 content");
    addComment(id1, "Good topic");
    addComment(id2, "This is a comment");
    addComment(id2, "This is another comment");            
  9. Replace the http.createServer function with the following code:
    var server = http.createServer(function (request, response) {
        response.setHeader('Access-Control-Allow-Origin', '*');
        response.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
        response.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
        response.setHeader('Access-Control-Allow-Credentials', true);
    
        console.log('TopicList=' + JSON.stringify(topicList));
        console.log('TopicDetail=' + JSON.stringify(topicDetail));
        var requestBody = '';
    
        request.on('data', function (data) {
            requestBody += data;
        });
    
        request.on('end', function () {
            handleRequest(request, response, requestBody);
        });
    });                    
  10. Add this function to handle the HTTP requests:
    function handleRequest(request, response, requestBody) {
        console.log(request.method + ":" + request.url + ' >>' + requestBody);
    
        if (request.url == '/') {
            if (request.method == 'POST') {
                var jsonMsg = JSON.parse(requestBody);
                addTopic(jsonMsg.title, jsonMsg.text);
                response.end();
            } else {
                response.end(JSON.stringify(topicList));
            }
        } else {
            var topicId = request.url.substring(1);
            if (request.method == 'POST') {
                var jsonMsg = JSON.parse(requestBody);
                addComment(jsonMsg.topicId, jsonMsg.text);
                response.end();
            } else {
                response.end(JSON.stringify(topicDetail[topicId]));
            }
        }
    }           
  11. Your application must listen to requests on a port provided by an Oracle Application Container Cloud Service environment variable. In your server.js file, update the var PORT variable declaration.
    var PORT = process.env.PORT || 80;                         

part 2 Prepare the Package for deployment

Oracle Application Container Cloud Service requires a manifest.json file, which contains information about which Node.js command the service should run.

  1. Create a manifest.json file and add:
    {
       "runtime":{
       "majorVersion":"4"
       },
       "command": "node server.js",
       "release": {},
       "notes": ""
    }
  2. Compress the server.js and manifest.json files and bundle them into a single zip file named sample.zip.

part 3Deploy the Sample Application to Oracle Application Container Cloud Service

  1. Open the Oracle Application Container Cloud Service console.
  2. On the Applications list view, click Create Application.
  3. On the Create Application page, click Node.
  4. In the Application section, enter Sample for the name of your application, and click Browse.
  5. In the File Upload dialog box, select the sample.zip file, and click Open.
  6. Keep the default values in the Instances and Memory fields and click Create.

part 4 Test your Node.js RESTful Service using cURL

  1. On the Applications tab, click Refresh repeatedly until your application is created.
  2. Copy the application URL.
    URL on the application detail page
    Description of the illustration test-sample-accs-07.jpg
  3. In a Git CMD window, access the URL as a REST endpoint:
  4. curl -i -X GET application URL

    The sample data that you entered in server.js is displayed.

    HTTP/1.1 200 OK
                    Server: Oracle-Traffic-Director/11.1.1.9
                    Date: Fri, 07 Apr 2017 18:27:41 GMT
                    Access-control-allow-origin: *
                    Access-control-allow-methods: GET, POST, OPTIONS, PUT, PATCH, DELETE
                    Access-control-allow-headers: X-Requested-With,content-type
                    Access-control-allow-credentials: true
                    Content-length: 59
                    Via: 1.1 net-apaasotd
                    Proxy-agent: Oracle-Traffic-Director/11.1.1.9
                    
                    [{"title":"Topic 1","id":124},{"title":"Topic 2","id":125}]            
  5. Add a message.
  6. curl -i -X POST -H "Content-Type: application/json" -d '{"title":"Hello", "id":126}' application URL
  7. Repeat step 2. The sample data is updated.
  8. HTTP/1.1 200 OK
                    Server: Oracle-Traffic-Director/11.1.1.9
                    Date: Wed, 12 Apr 2017 17:19:25 GMT
                    Access-control-allow-origin: *
                    Access-control-allow-methods: GET, POST, OPTIONS, PUT, PATCH, DELETE
                    Access-control-allow-headers: X-Requested-With,content-type
                    Access-control-allow-credentials: true
                    Content-length: 86
                    Via: 1.1 net-apaasotd
                    Proxy-agent: Oracle-Traffic-Director/11.1.1.9
                    
                    [{"title":"Topic 1","id":124},{"title":"Topic 2","id":125},{"title":"Hello","id":126}]
                    

more informationWant to Learn More?