一个GET请求不起作用但其他人工作

时间:2018-03-06 15:46:37

标签: node.js express ejs

我从我的其他GET请求中复制/粘贴了几乎所有内容,但是" / creation / add"一个人没有,我不明白为什么。

我尝试过很多东西,例如删除GET方法的内容,只是测试一个控制台打印,但这也没有用。我也尝试将其切换到POST方法并获得相同的结果。有一种叫做" / creation"的POST方法。所以我想也许它是" / add"这会导致问题,但即使将名称换成不同的名称也不起作用。

app.js

    var express = require('express');
    var app = express();
    var mysql = require('mysql');
    var bodyParser = require('body-parser');

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

    app.listen(3000, function () {
        console.log('Server address: http://localhost:3000')
    });

    app.use('/node_modules', express.static(__dirname + '/node_modules'));
    app.use('/style', express.static(__dirname + '/style'));
    app.use('/views/pages', express.static(__dirname + '/views/pages'));
    app.use('/views/partials', express.static(__dirname + '/views/partials'));
    app.set('view engine', 'ejs');

app.get('/creation/add'), function (req, res) {
    var question = req.query.questions;
    var reponse = req.query.reponses;
    var query = "INSERT INTO `" + selectedCour + "` VALUES (NULL, `" + question + "`, `" + reponse + "` CURDATE())";

    if (!/^[A-Z0-9 ]*$/.test(question) && !/^[A-Z0-9 ]*$/.test(reponse)) {
        connection.query(query, function (err, rows) {
            if (!err) {
                currentExamJson = JSON.parse(JSON.stringify(rows));
            }
        });
    }
    res.render('pages/creationExam', {data: currentExamJson});
};

html表单

<div class="add-container">
            <div style="height:100%;width:100%;float: right;">
                <form class="form-addQ" method="get" action="/creation/add">
                    <input type="text" required="" name="questions" class="text-linex"
                           placeholder="question">
                    <input type="text" required="" name="responses" class="text-linex"
                           placeholder="response">
                    <button id="btnAddQ" style="margin-top: 12px;" class="btn btn-small btn-primary btn-block" type="submit">add
                    </button>
                </form>
            </div>
        </div>

1 个答案:

答案 0 :(得分:0)

您没有在app.get中传递路由处理程序。

   app.get('/creation/add',
        function (req, res) {
            var question = req.query.questions;
            var reponse = req.query.reponses;
            var query = "INSERT INTO `" + selectedCour + "` VALUES (NULL, `" + question + "`, `" + reponse + "` CURDATE())";

            if (!/^[A-Z0-9 ]*$/.test(question) && !/^[A-Z0-9 ]*$/.test(reponse)) {
                connection.query(query, function (err, rows) {
                    if (!err) {
                        currentExamJson = JSON.parse(JSON.stringify(rows));
                    }
                });
            }
            res.render('pages/creationExam', {
                data: currentExamJson
            });
        });