为什么请求正文内容为空?

时间:2019-02-26 02:36:46

标签: node.js node-modules

当我提交注册表时,该注册表页面中的数据应发布在控制台上。我尝试使用“ req.body.Username”打印该结果,它显示为undefined,当我在其中查看req时安慰。正文似乎是一个空集,例如“ {}”,如何将我的表单详细信息发布到正文中以在控制台中查看以及如何摆脱该“未定义”错误?

app.js

const express = require('express');
const flash = require('connect-flash');
const path = require('path');
const request = require('request')
const expressValidator = require('express-validator');
const authRoutes = require('./routes/auth-routes');
const session = require('express-session');
const passport = require('passport');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const db = require('./config/db');
const registerc = require('./registerc');
const loginc = require('./loginc');
const registerRoute = require('./routes/register');
const fs = require('fs');

// const request = require('./modules/module1')
const app = express();

// set view engine
app.set('view engine', 'pug');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// set up routes
app.use('/auth', authRoutes);
app.use('', registerRoute);

// create home route
app.get('/', (req, res) => {
            res.render('home');
            res.render('mycourses', {
                final1: final1
            });
            res.render('recent', {
                final2: final2
            });
        });


// fetching course details

request(options, function (error, response, result) {
    if (error) throw new Error(error);
    // console.log(result);
    // final1 = JSON.stringify(result)
    final1 = JSON.parse(result);
    // console.log(final1);
});

// fetching User recent Activity

request(options, function (error, response, result) {
    if (error) throw new Error(error);
    // console.log(result);
    final2 = JSON.parse(result);
    // console.log(final2);
    //   console.log(final2.length)
});

app.use(session(
    {
        secret : 'secret',
        saveUninitialised : true,
        resave : true
    }
));

app.use(passport.initialize());
app.use(passport.session());


app.use(expressValidator({
    errorFormatter: function(param, msg, value) {
        var namespace = param.split('.')
        , root    = namespace.shift()
        , formParam = root;

      while(namespace.length) {
        formParam += '[' + namespace.shift() + ']';
      }
      return {
        param : formParam,
        msg   : msg,
        value : value
      };
    }
}));

app.use(flash());    

// app.use(function(res,req,next){
//  res.locals.success_msg = req.flash('success_msg');
//  res.locals.error_msg = req.flash('error_msg');
//  res.locals.error = req.flash('error');
//  next();
// })

// app.post('/api/register',registerc.register);
// app.post('/api/login', loginc.login);



app.listen(3000, () => {
    console.log('app now listening for requests on port 3000');
});

1 个答案:

答案 0 :(得分:0)

在您的app.js中,从

更新注册身份验证
app.use('', registerRoute);

收件人

app.use('/soemRoute', registerRoute);