opentok/opentok-node

Missing view engine in hello world sample

Opened this issue · 1 comments

There was things missing in the helloworld sample:

For example view engine:
// set the view engine to ejs
app.set('view engine', 'ejs');

But after some hacking I got it working on Heroku.

I also moved the lib dir to HelloWorld and had to add the path dependency. Then I added the dependencies to the package.json:
"dependencies": {
"ejs": "^2.5.5",
"express": "^3.5.0",
"jsonwebtoken": "^7.4.1",
"lodash": "^4.17.4",
"opentok-token": "^1.1.0",
"path": "^0.12.7",
"request": "^2.72.0"
}

Added Procfile, with the content: web: node index.js

And config setting thru the cli:
heroku config:set API_KEY="XXXXX"

heroku config:set API_SECRET="XXX....XXXX"

´´´
// Dependencies
var express = require('express'),
path = require('path'),
OpenTok = require('./lib/opentok');

// Verify that the API Key and API Secret are defined
var apiKey = process.env.API_KEY,
apiSecret = process.env.API_SECRET;
if (!apiKey || !apiSecret) {
console.log('You must specify API_KEY and API_SECRET environment variables');
process.exit(1);
}

// set the port of our application
// process.env.PORT lets the port be set by Heroku
var port = process.env.PORT || 3000;
// Initialize the express app
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));

// Initialize OpenTok
var opentok = new OpenTok(apiKey, apiSecret);

// Create a session and store it in the express app
opentok.createSession(function(err, session) {
if (err) throw err;
app.set('sessionId', session.sessionId);
// We will wait on starting the app until this is done
init();
});

app.get('/', function(req, res) {
var sessionId = app.get('sessionId'),
// generate a fresh token for this client
token = opentok.generateToken(sessionId);

res.render('index.ejs', {
apiKey: apiKey,
sessionId: sessionId,
token: token
});
});

// Start the express app
function init() {
app.listen(port, function() {
console.log('Our app is running on http://localhost:' + port);
});
}

´´´

aiham commented

Hi @appernetic

  1. Express.js documentation states:

If the view engine property is not set, you must specify the extension of the view file.

Since we specify .ejs in the render function argument then we don't need to set the view engine.

  1. We don't set the views setting because we assume you are running the sample from within the HelloWorld/ directory as described in the README. By default it is set to process.cwd() + '/views': https://expressjs.com/en/4x/api.html#app.set

  2. You don't need to include path in your package.json file because it's include in the standard node API: https://nodejs.org/api/path.html

  3. We need to keep lib/ outside HelloWorld/ because it's not really part of the samples. I admit the current setup isn't ideal for Heroku deployment without changes. This could certainly be improved.

Thanks for the feedback. Closing this issue for now.