DevOps

Deploy Oracle JET Web App on Application Container Cloud

There’s a few examples on the net about how to deploy a JET app to Application Container Cloud. All of these seem to either create an express app and then add oracle code to it later (eliminating the benefit of using grunt serve) or taking the compiled web folder and putting it into a new express application (making your devops or build automation slightly more painful). Let me show you an alternative.

Add the following to server.js in the root directory.

var http = require('http')
var url = require('url')
var fs = require('fs')
var path = require('path')
var baseDirectory = __dirname + '/web'

var port = process.env.PORT || 8000;

http.createServer(function (request, response) {
	try {
		var requestUrl = url.parse(request.url)
		if (requestUrl.pathname == '/') requestUrl.pathname = '/index.html';
  	// need to use path.normalize so people can't access directories underneath baseDirectory
   	var fsPath = baseDirectory+path.normalize(requestUrl.pathname)

  	response.writeHead(200)
  	var fileStream = fs.createReadStream(fsPath)
   		fileStream.pipe(response)
   		fileStream.on('error',function(e) {
    	response.writeHead(404)     // assume the file doesn't exist
    	response.end()
  	})
 	} catch(e) {
 		response.writeHead(500)
   	response.end()     // end the response so browsers don't hang
   	console.log(e.stack)
 	}
}).listen(port)

This uses the basic http module in Node to create a very simple http server to serve our JET code from the web directory. If you run this using node server.js you should see your node app running, just like if you used grunt serve

You will also need to create a manifest.json file (and optional deployment.json file) to ensure ACC understands your request. The manifest file should look something like this:

{
	"runtime" : {
		"majorVersion": "0.12"
	},
	"command" : "node server.js",
	"release" : {},
	"notes" : "Notes"
}

Now just upload this to ACC (use github.com/Joelith/acc-deploy to make life easier for yourself).

This approach means you can continue to run grunt serve locally as part of your build process and you can still use the same app folder to deploy to ACC.