当我转到特定的路由时,即使它不存在,也会呈现错误的ejs文件。 NodeJs

问题描述 投票:0回答:1

我在调查结束时使用按钮来运行POST操作。该路由的POST操作仅执行目前操作,仅重定向到“谢谢”页面。路由时,将呈现空白的调查EJS文件,而不是感谢ejs文件。即使我将POST路由重定向到一个尚未定义的路由,也会发生这种情况。我可以使用“ res.redirect('survey / ladkjfdalfkj')”代替“谢谢”页面,然后显示空白调查页面。

我已经尝试过仅通过转到“谢谢”页面的链接来替换“发布”按钮,但仍呈现错误的文件。我什至建立了到不存在的路由的链接,即使由于该路由不存在而出现错误,它仍会呈现。

这里是app.js

const express = require('express');
const chalk = require('chalk');
const debug = require('debug')('app');
const morgan = require('morgan');
const path = require('path');
var bodyParser = require('body-parser');


const app = express();
const port = process.env.PORT || 80;

app.use(bodyParser.urlencoded({extended:true}));

app.use(morgan('tiny'));
app.use(express.static(path.join(__dirname, 'public')));
//app.use('/css', express.static(path.join(__dirname, '/node_modules/bootstrap/dist/css')));
//app.use('/js', express.static(path.join(__dirname, '/node_modules/bootstrap/dist/js')));
//app.use('/js', express.static(path.join(__dirname, '/node_modules/jquery/dist')));
app.use('/js', express.static(path.join(__dirname, '/js')));
app.use('/css', express.static(path.join(__dirname, '/css')));
app.set('views', './src/views');
app.set('view engine', 'ejs');

const surveyRouter = require('./src/routes/surveyRoutesTest')();

app.use('/survey', surveyRouter);

app.route('/')
  .get((req, res) => {
    res.send('Access Forbiden')
  });

app.listen(port, () => {
  debug('listening on port ', chalk.green(port));
});

这里是surveyRoutes.js

const express = require('express');
const debug = require('debug');
const surveyRouter = express.Router();
const mysql = require('mysql');
var Q = require('q');
require('dotenv').config();

const connection = mysql.createConnection({
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
  host: process.env.DB_HOST
});


connection.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});


function router() {

  surveyRouter.get('/:surveyGuid/', function(req, res){
    //store parameters
    var surveyGuid = req.params.surveyGuid;
    var prms = req.query;
    var date = prms.s;
    var dmsId = prms.d;

     Q.ninvoke(connection, "query", 'CALL spGetSurveyQuestions(?);', surveyGuid)
      .then(function(rows){
          // console.log(rows[0]);
          questions = rows[0][0];
      })
      .then(function(rows){
        Q.ninvoke(connection, "query", "CALL spGetSurveyQuestionValuesBySurvey(?)", surveyGuid)
        .then(function(rows){
          questionValues = rows[0][0]
          res.render("surveyQuestionsTest", {questions: questions, questionValues:questionValues, surveyGuid: surveyGuid, prms: prms})
        })
      })
      .catch(function(error){
        console.log(error);
        res.writeHead(500);
        res.end();
      })
      .done();
  });


  surveyRouter.post('/:surveyGuid', function(req, res){
    var surveyGuid = req.params.surveyGuid;
    var prms = req.query;
    var date = prms.s;
    var dmsId = prms.d;
    res.redirect("/survey/myself")
  });

  surveyRouter.get('/thankyou', function(req, res){
    res.send("hello world");
  });

  return surveyRouter;
}

module.exports = router;

我从中删除了其他一些路由,但是它们工作正常,并且与错误无关。

如您所见,thankyou页面仅应显示“ Hello world”,但其在res.render('surveyQuestionsTest)处呈现该页面

这里是surveyQuestionsTest.ejs

  <%include partials/header%>

<div id="masthead" class="container">
      <div class="row py-2 shadow">
        <div class="col-md-12 my-3">
          <h1 class=text-center>Dental Patient Satisfaction Survey</h1>
        </div>
      </div>
</div>

<form action="/survey/<%=surveyGuid%>?d=<%=prms.d%>&s=<%=prms.s%>" method="POST">
  <div class="containter">
    <div class="row justify-content-center">
      <div class="col-md-8">
        <div class="mx-5">
          <div id="mainDiv" class="form-group">

            <%questions.forEach(function(question){%>
              <div class="row justify-content-left my-2 border-bottom">
                <label class="question">
                  <strong><span id="questionNumber"><%=question.QustionSequence%>.</span> <%=question.QuestionText%></strong></br>
                </label>
                <!-- DROP DOWN HANDLING -->
                <%if(question.QuestionType == "DropDown"){%>
                  <select  class="form-control my-2 shadow-sm" name="<%=question.QustionSequence%>">
                    <%questionValues.forEach(function(questionValue){%>
                      <%if(questionValue.QuestionId == question.QuestionId){%>
                          <option required value="<%=questionValue.AnswerValue%>,<%= questionValue.AnswerText%>,<%= question.QuestionGuid%>,<%= question.QuestionType%>">
                            <%=questionValue.AnswerText%>
                          </option>
                      <%}%>
                    <%});%> 
                  </select>   
                <%}%>

                <!-- RADIO BUTTON HANDLING -->
                <%if(question.QuestionType == 'RadioButton'){%>
                  <div class="container">
                    <%questionValues.forEach(function(questionValue){%> 
                      <div class="row justify-content-left">
                        <%if(questionValue.QuestionId == question.QuestionId){%>
                            <label class="px-3" >
                              <input class="mx-2" 
                              type="radio" name="<%=question.QustionSequence%>" 
                              value="<%=questionValue.AnswerValue%>,<%= questionValue.AnswerText %>,<%= question.QuestionGuid%>,<%= question.QuestionType%>" 
                              > 
                              <%= questionValue.AnswerText%>
                            </label>
                          </br>
                        <%}%>
                      </div>
                    <%});%>
                  </div>
                <%}%>

                <!-- TEXT BOX HANDLING -->
                <%if(question.QuestionType == 'Text'){%>
                  <div class="container">
                    <%questionValues.forEach(function(questionValue){%> 
                    <%if(questionValue.QuestionId == question.QuestionId){%>
                      <input type="hidden" name="<%=question.QustionSequence%>" value="True,<%= questionValue.AnswerValue %>,<%= question.QuestionType%>,
                      <%= question.QuestionGuid%>">
                      <div class="row justify-content-center shadow-sm">
                        <textarea name="<%=question.QustionSequence%>Text" class="form-control" rows="3"></textarea>    
                      </div>
                    <%}%>
                  <%});%>
                  </div>
                <%}%>

              </div>
            <%});%>


          </div>
          <div class="row justify-content-center">
            <button id="nextButton" class="btn btn-block btn-primary" type="submit">Submit</button>
            <!-- <a href="/survey/thankyou">Submit</a> -->
          </div>
        </div>
      </div>
    </div>
  </div>
</form>


<%include partials/footer%>

如果我注释掉按钮并使用该Submit链接,我仍然会遇到相同的问题。而且,如果该链接指向不存在的路由,它仍将呈现空白的SurveyQuestionsTest.ejs文件,而不显示错误。

我应该得到一个谢谢页面,但是我没有。如果我使/ survey / surveyGuid路由呈现“感谢”页面,则“感谢”页面会很好地呈现。

我认为它在SurveyQuestionsTest.ejs中有用,但老实说我不确定。我对做些什么很迷茫。如果您需要更多信息,请告诉我。

mysql node.js express ejs
1个回答
0
投票

非常非常愚蠢...谢谢路线被解释为/:surveyGuid路线...

© www.soinside.com 2019 - 2024. All rights reserved.