使用Express + Bootstrap模式时POST无法正常工作

时间:2019-03-01 12:46:38

标签: node.js express pug

我使用了引导程序模版制作了一个注册表,但似乎无法使POST请求生效。

这是我的en.pug文件中的表格

#register.modal.fade(tabindex="-1" role="dialog" aria-labelledby="register form" aria-hidden="true")
        .modal-dialog(role="document")
            .modal-content
                .modal-header
                    h5#exampleModalLabel.modal-title Sign Up
                    button.close(type="button" data-dismiss="modal" aria-label="Close")
                        span(aria-hidden="true") ×
                .modal-body
                    form(action='/register' method='POST' )
                        .form-group
                            label.form-control-label(for="name") Name:
                            input#name.form-control(type="text", placeholder='first and last' name='name')
                        .form-group
                            label.form-control-label(for="email") Email:
                            input#email.form-control(type="email", placeholder='name@email.com', name='email')
                        .form-group
                            label.form-control-label(for="password") Password:
                            input#password.form-control(type="password" name='password')
                        .form-group
                            label.form-control-label(for="password") Confirm Password:
                            input#confirmed-password.form-control(type="password" name='confirmPassword')
                        hr.mb-4
                    // /registration form
                        .modal-footer
                             button.btn.btn-secondary(type="button" data-dismiss="modal") Close
                             button.btn.btn-primary(type="submit") Sign Up

这是我的server.js文件

const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();

// serve static files from /public
app.use(express.static(__dirname + '/public'));
app.use(cookieParser());

// view engine setup
app.set('view engine', 'pug');
app.set('views', __dirname + '/views');

// include routes
const routes = require('./routes/index');
app.use('/', routes);


app.listen(3000);

这是./routes/index.js

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

router.get('/', (req, res) => {
    const cookie = req.cookies.language;
    if (cookie) {
        res.render('en', { cookie });
    } else {
        res.render('ro');
    }
});

router.get('/en', function (req, res) {
    res.cookie('language');
    return res.render('en');
});

// GET /
router.get('/ro', function(req, res) {
    res.clearCookie('language');
    return res.render('ro');
});

app.post("/register", function (req, res) {
    console.log('Hellooooooooooooooooo!')
});

module.exports = router;

当我填写表格并按“注册”按钮时,我希望显示“ Hellooooooooooooooooo!”。在控制台中,但没有任何反应,我似乎无法弄清楚原因。我在这里想念东西吗?

编辑:我发现我在帕格缩进中犯了一个错误,并且“提交”按钮不在表单之外,填写表单和按“注册”按钮的原因无济于事

2 个答案:

答案 0 :(得分:2)

您必须使用df.append(dff, ignore_index=True, sort=False)

在Express 4版本更新之前,我们必须安装bodyparser middleware,但是Express现在默认支持body-parser

只需添加

body-parser

希望有帮助。

答案 1 :(得分:-1)

//install body-parser npm and require it
const bodyParser = require('body-parser')

//write code below of app.use(cookieParser()) in server.js

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

router.post('/', function(req, res){
 console.log(req.body) //here you will get POST data
})