Before 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?
- Node.js
- Git (Git CMD shell to execute cURL commands)
- Access to an instance of Oracle Application Container Cloud Service
- A storage replication policy for your service instance
Develop a Sample RESTful Node.js Service
- Create a JavaScript file and name it
server.js.
- 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
port8089
. In a later step, you will change this port with an Oracle Application Container Cloud Service variable. - Test your server.
node server.js
- In a browser window, go to
http://localhost:8089
and look for the following message:"LATER ON, YOU WILL PLACE CODE HERE."
- To stop the server, press CTRL+C.
- Add the following variable declarations after the
var PORT
declaration:var topicList = []; var topicDetail = {}; var currentId = 123;
- 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); }
- 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");
- 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); }); });
- 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])); } } }
- 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 thevar PORT
variable declaration.var PORT = process.env.PORT || 80;
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.
- Create a
manifest.json
file and add:{ "runtime":{ "majorVersion":"4" }, "command": "node server.js", "release": {}, "notes": "" }
- Compress the
server.js
andmanifest.json
files and bundle them into a single zip file namedsample.zip.
Deploy the Sample Application to Oracle Application Container Cloud Service
- Open the Oracle Application Container Cloud Service console.
- On the Applications list view, click Create Application.
- On the Create Application page, click Node.
- In the Application section, enter
Sample
for the name of your application, and click Browse. - In the File Upload dialog box, select the
sample.zip
file, and click Open. - Keep the default values in the Instances and Memory fields and click Create.
Test your Node.js RESTful Service using cURL
- On the Applications tab, click Refresh repeatedly until your application is created.
- Copy the application URL.
Description of the illustration test-sample-accs-07.jpg - In a Git CMD window, access the URL as a REST endpoint:
- Add a message.
- Repeat step 2. The sample data is updated.
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}]
curl -i -X POST -H "Content-Type: application/json" -d '{"title":"Hello", "id":126}' application URL
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}]
Want to Learn More?
- Node.js website nodejs.org
- Using Oracle Application Container Cloud Service