【问题标题】:Error when trying to serve ejs file through express node.js尝试通过 express node.js 提供 ejs 文件时出错
【发布时间】:2017-03-02 02:56:09
【问题描述】:

我正在尝试通过快递服务.ejs file,但我收到此错误

错误:未指定默认引擎且未提供扩展名。 在新视图 (C:\development\dashboard\node_modules\express\lib\view.js:62:11) 在 EventEmitter.render (C:\development\dashboard\node_modules\express\lib\application.js:569:12) 在 ServerResponse.render (C:\development\dashboard\node_modules\express\lib\response.js:961:7) 在 C:\development\dashboard\app.js:51:7
在 Layer.handle_error (C:\development\dashboard\node_modules\express\lib\router\layer.js:71:5) 在 trim_prefix (C:\development\dashboard\node_modules\express\lib\router\index.js:310:13) 在 C:\development\dashboard\node_modules\express\lib\router\index.js:280:7 在 Function.process_params (C:\development\dashboard\node_modules\express\lib\router\index.js:330:12) 在下一个(C:\development\dashboard\node_modules\express\lib\router\index.js:271:10) 在 Layer.handle_error (C:\development\dashboard\node_modules\express\lib\router\layer.js:67:12)

这是我的文件

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';
        }
    }
}();

当我将 index.js 文件更改为只返回一些文本时,它可以正常工作。像 res.send('Hello'); 而不是 res.send(compiled({runOnEnv : EnvConfig.getProcessEnv()})); 我得到了结果。当我在 index.js 中执行 console.log(content); 时,我什至可以看到 index.ejs 文件

非常感谢任何帮助。

【问题讨论】:

    标签: javascript node.js express ejs


    【解决方案1】:

    出现错误是因为您的 EnvHandler.js 没有导出任何内容。您需要在EnvHandler.js 中导出module.exports = config 之类的函数

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 2012-07-23
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多