繁体   English   中英

Express.js HTTP 500错误:页面与res.send(“ Some text”)一起使用; 但是使用res.render('file')时会导致错误吗?

[英]Express.js HTTP 500 ERROR: Page works with res.send(“Some text”); but does error caused when using res.render('file');

我有一个包含路由/ request的页面,其中包含一个表单。 表单方法是POST,操作是/ request request.js中的POST处理程序中应该发生的事情是对表单数据(存储在DB等中)进行处理,然后使用route / trips重定向到另一个页面。

每当我使用res.send(“ Some text here”); GET处理程序都可以正常工作。 但是当我尝试使用res.render('trips')渲染页面时,却给我一个HTTP 500 Internal server error。

这是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响应!” 当我访问localhost:8000 / trips时(从导航栏或通过提交表单)

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:本地主机当前无法处理此请求。 HTTP错误500(获取请求)

我建议将您在此处发布的代码更改为此:

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

res.render用于使用某些模板引擎(如小胡子,玉石等)渲染静态模板文件。

因此,您应该配置模板引擎,创建一个静态文件,然后res.render('trips')解释并渲染模板。

您可以在此处找到有关如何快速使用模板引擎的信息。

尝试使用本教程来捕获您的错误:

在此示例中,通用logError可能将请求和错误信息写入stderr,例如:

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

另外,您可以使用try-catch语句。 在这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM