2017-11-25 161 views
0

我有一個包含路徑/請求的頁面,其中包含一個表單。表單方法是POST,操作是/請求。在POST處理程序request.js中應該發生的事情是在表單數據(存儲在DB等)中執行某些操作,然後重定向到與路由/旅行不同的頁面。Express.js HTTP 500錯誤:頁面可用於res.send(「某些文本」);但是在使用res.render('file')時會導致錯誤;

當我使用res.send(「某些文本在這裏」)時,GET處理程序正常工作;但是當我嘗試使用res.render('trips')渲染頁面時,例如給我一個HTTP 500內部服務器錯誤。

下面是request.js代碼:

var router = require('express').Router(); 


router.get('/', checkLoggedIn, function (req, res) { 
    res.render('request', { 
     user: req.user // get the user out of session and pass to template 
    }); 
}); 

router.post('/', checkLoggedIn, function (req, res) { 
    console.log(req.body); //data should be stored in DB 
    res.redirect('/trips'); //Should redirect to /trips after form 
submission. Why ERROR 500? 
}); 

function checkLoggedIn(req, res, next) { 
    if (req.isAuthenticated()) { 
     return next(); 
    } 
    //Redirect to home if not logged in 
    res.redirect('/'); 
} 

module.exports = router; 

下面是trips.js代碼:

var router = require('express').Router(); 

router.get('/', checkLoggedIn, function(req, res) { 
    res.send("This is the trips route GET response!"); 
}); 

所以上述部分工作並打印「這是旅行路線GET迴應!「當我訪問本地主機:8000 /車次(從導航欄或通過提交表單)

router.get('/', checkLoggedIn, function(req, res) { 
    res.render('trips'); 
}); 

然而,當我寫這篇文章,而不是它給了我一個HTTP 500錯誤本地主機是目前無法處理此請求。

function checkLoggedIn(req, res, next) { 
    if (req.isAuthenticated()) { 
     return next(); 
    } 
    //Redirect to home if not logged in 
    res.redirect('/'); 
} 

module.exports = router; 

這裏是我的trips.ejs(上下文)文件:

<!DOCTYPE html> 

<html lang="en"> 

<head> 

    <meta charset="utf-8"> 
    <!--<title><%= user.firstName %>'s Trips - Erkab</title>--> 

    <!--<meta name="viewport" content="width=device-width, initial-scale=1">--> 
    <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0"/> 
    <meta name="apple-mobile-web-app-capable" content="yes"/> 

    <link rel="stylesheet" href="/public/css/base.css"> 
    <link rel="stylesheet" href="/public/css/main.css"> 
    <link rel="stylesheet" href="/public/css/erkab.css"> 


    <script src="/public/js/modernizr.js"></script> 

</head> 

<body id="top" style="width: 100%"> 

<% include templates/header.ejs %> 

<% if (userType != "Rider") { 
    userType = "Driver"; 
} %> 

<div id="changeableView" class="container-fluid"> 
     <table class="table table-hover"> 
     <thead class="thead-inverse"> 
     <tr> 
      <th>#</th> 
      <th>USER TYPE</th> 
      <th>LOCATION</th> 
      <th>DATE</th> 
      <th>TIME</th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr> 
      <th scope="row">1</th> 
      <td><%= userType %></td> 
      <td><%= points %> - <%= area %></td> 
      <td><%= date %></td> 
      <td><%= time %></td> 
     </tr> 
     </tbody> 
    </table> 
</div> 

<script src="/public/js/jquery-2.1.3.min.js"></script> 
<script src="/public/js/main.js"></script> 
</body> 
</html> 
+1

你看到什麼錯誤記錄在節點控制檯?這可能與EJS文件試圖使用未傳入的變量一樣簡單。您可以嘗試將該文件的內容更改爲「Hello」以查看是否成功呈現。如果沒有,也許你可以發佈你用來配置「視圖」和「視圖引擎」設置的代碼,這樣我們可以仔細檢查你是否已經正確設置了這些設置? – skirtle

+0

您似乎已經提出了相同的問題兩次https://stackoverflow.com/questions/47486074/express-js-localhost-is-currently-unable-to-handle-this-request-http-error-500 – skirtle

回答

1

模板引擎信息一些有關此問題的相關信息,包括在其他複製你貼:

Express.js: localhost is currently unable to handle this request. HTTP ERROR 500 (GET Request)

我會建議將您在此發佈的代碼更改爲:

router.get('/', checkLoggedIn, function(req, res) { 
    res.render('trips', { 
     user: req.user, 
     userType: 'Driver', 
     area: null, 
     points: null, 
     date: null, 
     time: null, 
     driverPref: null, 
    }); 
}); 

我不認爲你是我需要所有這些,但是一旦渲染就可以收拾起來。

我懷疑這裏有兩個真正的問題。其一是這一行:

<!--<title><%= user.firstName %>'s Trips - Erkab</title>--> 

這「註釋」不會阻止EJS嘗試訪問user.firstName。就EJS而言,註釋只是輸出字符串的一部分,不會被視爲任何特殊的東西。

還有這個:

<% if (userType != "Rider") { 

我相信也將失敗作爲userType不會被宣佈。

+0

非常感謝您的幫助@skirtle。這確實是導致錯誤的EJS文件中使用的未被超越的變量。例如,我認爲頁面應該呈現爲空值,但事實並非如此。 –

-1

嘗試使用this tutorial趕上你的錯誤:

In this example, the generic logErrors might write request and error information to stderr, for example:

function logErrors (err, req, res, next) { console.error(err.stack) next(err) } 

或者,你可以使用try-catch聲明。 here

+1

對不起,我不認爲這真的回答了這個問題 –

+0

我並不真正在尋找錯誤處理。我很想知道錯誤是什麼原因以及如何解決。 –

+0

我明白了,那麼你可以使用'try-catch'來找出你得到的錯誤。 – Azuloo

0

res.render使用某些模板引擎就像鬍子來渲染靜態模板文件,玉石等。

所以,你應該配置模板引擎,創建一個靜態文件,然後res.render(「旅行」)解釋並呈現模板。

你可以找到關於如何使用快遞here

+0

這很明顯。我已經做到了。在我的情況下,它應該呈現一個**。ejs **文件。 –

+0

如何配置視圖?什麼是目錄結構? – filippo

+0

他們都在_views目錄_ –