Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 为特定URL加载不同的ejs文件_Node.js_Ejs - Fatal编程技术网

Node.js 为特定URL加载不同的ejs文件

Node.js 为特定URL加载不同的ejs文件,node.js,ejs,Node.js,Ejs,我有一个登录页面,然后在登录页面后面有多个其他页面。我使用Express并选择ejs作为我的模板引擎,我创建了2个ejs文件:1个用于我的登录页面,1个用于我的其余页面 我的app.js app.get如下所示: app.get('/', function (req, res) { res.render("login", { client_id: client_id, app_url: app_url}); }); app.get('*', function (req, res) {

我有一个登录页面,然后在登录页面后面有多个其他页面。我使用Express并选择ejs作为我的模板引擎,我创建了2个ejs文件:1个用于我的登录页面,1个用于我的其余页面

我的app.js app.get如下所示:

app.get('/', function (req, res) {
    res.render("login", { client_id: client_id, app_url: app_url});
});
app.get('*', function (req, res) {
    res.render("index", { client_id: client_id, app_url: app_url});
});
我的控制器加载登录页面,然后加载仪表板

function config($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise("/");
     $stateProvider
        .state('login', {
            url: "/",
            templateUrl: "views/login.html",

    })

    $stateProvider
        .state('dashboard_1', {
            url: "/dashboard",
            templateUrl: "views/dashboard.html",
    })

当我导航到/时,必须加载login.ejs;当我导航到任何其他url时,必须加载indes.ejs。如何使其工作?

*
将适用于所有URL,包括
/
,因此您可以这样做

app.get('*', function (req, res) {
    if ( req.path == '/'){
       res.render("login", { client_id: client_id, app_url: app_url});
    }
    else{
       res.render("index", { client_id: client_id, app_url: app_url});
    }
});

感谢您的回复,但似乎req.path总是=='/'我记录了req.path并确认了。为什么不解析路径?如果点击URL
http://host:port/
,则
请求url
将是
/
,如果您转到
http://host:port/index
,然后,
req.url
将成为
/index
,因为我使用的url看起来像
http://host:port/#/
用于登录,其余将为
http://host:port/#/*