0
votes

I am trying to serve an .ejs file through express but I am getting this error

Error: No default engine was specified and no extension was provided. at new View (C:\development\dashboard\node_modules\express\lib\view.js:62:11) at EventEmitter.render (C:\development\dashboard\node_modules\express\lib\application.js:569:12) at ServerResponse.render (C:\development\dashboard\node_modules\express\lib\response.js:961:7) at C:\development\dashboard\app.js:51:7
at Layer.handle_error (C:\development\dashboard\node_modules\express\lib\router\layer.js:71:5) at trim_prefix (C:\development\dashboard\node_modules\express\lib\router\index.js:310:13) at C:\development\dashboard\node_modules\express\lib\router\index.js:280:7 at Function.process_params (C:\development\dashboard\node_modules\express\lib\router\index.js:330:12) at next (C:\development\dashboard\node_modules\express\lib\router\index.js:271:10) at Layer.handle_error (C:\development\dashboard\node_modules\express\lib\router\layer.js:67:12)

Here are my files

index.ejs

<!DOCTYPE html>
<html>
<head>
    <title>ETL Dashborad</title>
    <script type="text/javascript" src="./resources/javascripts/jquery.min.js"></script>
    <script>
        window.GeorgeEnv = {};
        window.GeorgeEnv.serverRunOnEnv = "<%= runOnEnv %>";
    </script>
</head>
<body>
    <div>HELLO</div>
    <div id="app"></div>
    <script type="text/javascript" src="./js/client.min.js"></script>
</body>
</html>

index.js

var ejs = require('ejs');
var fs = require('fs');
var EnvConfig = require('../src/utils/EnvHandler.js');
module.exports = function(app, dirname) {

    var content = fs.readFileSync(dirname + '/src/index.ejs', 'utf-8');
    var compiled = ejs.compile(content);
    app.get("/", function(req, res){
        res.send(compiled({runOnEnv : EnvConfig.getProcessEnv()}));
    });

    app.all('/healthcheck', require('./routes/healthcheck'));
};

EnvHandler.js

const qa2Config = require('../resources/env-configs/qa2/env-config');
const prodConfig = require('../resources/env-configs/prod/env-config');

// Fetching the Env from the index.ejb file
var GeorgeEnv = GeorgeEnv || {};

var config = function() {
    var envInfo = [];
    envInfo['qa2'] = qa2Config.getEnvConfigs();
    envInfo['production'] = prodConfig.getEnvConfigs();
    var processEnv = process.env.NODE_ENV ? process.env.NODE_ENV : 'qa2';
    processEnv = process.env.NODE_ENV.trim();
    return {
        getSsoUrl: function(env) {
            if(env) {
                return envInfo[env].ssoUrl;
            }
            return envInfo[processEnv].ssoUrl;
        },
        getSelfUrl: function(env) {
            if(env) {
                return envInfo[env].selfUrl;
            }
            return envInfo[processEnv].selfUrl;
        },
        getServerUrl: function(env) {
            if(env) {
                return envInfo[env].serverUrl;
            }
            return envInfo[processEnv].serverUrl;
        },
        getRunOnPort: function(env) {
            if(env) {
                return envInfo[env].runOnPort;
            }
            return envInfo[processEnv].runOnPort;
        },
        getProcessEnv: function() {
            if(processEnv) {
                return processEnv;
            }
            return 'qa2';
        }
    }
}();

When I change my index.js file to only return some text it works fine. like res.send('Hello'); instead of res.send(compiled({runOnEnv : EnvConfig.getProcessEnv()})); I get the result. I can even see the index.ejs file when I do console.log(content); in my index.js

Any help is much appreciated.

1

1 Answers

0
votes

The error comes because your EnvHandler.js doesn't export anything. You need to export your function like module.exports = config in EnvHandler.js

EnvHandler.js

const qa2Config = require('../resources/env-configs/qa2/env-config');
const prodConfig = require('../resources/env-configs/prod/env-config');

// Fetching the Env from the index.ejb file
var GeorgeEnv = GeorgeEnv || {};

var config = function() {
    var envInfo = [];
    envInfo['qa2'] = qa2Config.getEnvConfigs();
    envInfo['production'] = prodConfig.getEnvConfigs();
    var processEnv = process.env.NODE_ENV ? process.env.NODE_ENV : 'qa2';
    processEnv = process.env.NODE_ENV.trim();
    return {
        getSsoUrl: function(env) {
            if(env) {
                return envInfo[env].ssoUrl;
            }
            return envInfo[processEnv].ssoUrl;
        },
        getSelfUrl: function(env) {
            if(env) {
                return envInfo[env].selfUrl;
            }
            return envInfo[processEnv].selfUrl;
        },
        getServerUrl: function(env) {
            if(env) {
                return envInfo[env].serverUrl;
            }
            return envInfo[processEnv].serverUrl;
        },
        getRunOnPort: function(env) {
            if(env) {
                return envInfo[env].runOnPort;
            }
            return envInfo[processEnv].runOnPort;
        },
        getProcessEnv: function() {
            if(processEnv) {
                return processEnv;
            }
            return 'qa2';
        }
    }
}();

module.exports = config;