Express.js HTTP 500错误:页面可用于res.send(“某些文本”);但是在使用res.render('file')时会导致错误;
我有一个包含路径/请求的页面,其中包含一个表单。表单方法是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>
模板引擎信息一些有关此问题的相关信息,包括在其他复制你贴:
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
不会被宣布。
非常感谢您的帮助@skirtle。这确实是导致错误的EJS文件中使用的未被超越的变量。例如,我认为页面应该呈现为空值,但事实并非如此。 –
尝试使用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。
对不起,我不认为这真的回答了这个问题 –
我并不真正在寻找错误处理。我很想知道错误是什么原因以及如何解决。 –
我明白了,那么你可以使用'try-catch'来找出你得到的错误。 – Azuloo
你看到什么错误记录在节点控制台?这可能与EJS文件试图使用未传入的变量一样简单。您可以尝试将该文件的内容更改为“Hello”以查看是否成功呈现。如果没有,也许你可以发布你用来配置“视图”和“视图引擎”设置的代码,这样我们可以仔细检查你是否已经正确设置了这些设置? – skirtle
您似乎已经提出了相同的问题两次https://*.com/questions/47486074/express-js-localhost-is-currently-unable-to-handle-this-request-http-error-500 – skirtle