Express路由器发布功能不起作用

时间:2017-05-12 12:17:05

标签: javascript node.js express

声明:

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

Route.js:在这里发布电话

router.route('/signup')
    .post(function (req, res) {
        console.log('post signup called', req.body);
        res.json({message: 'signup'});
    });

module.exports = router;

req.body始终未定义。我能够在ajax调用中打印它们的控制台。我不明白req.body未定义。我错过了什么?

发送的Ajax发布数据如下:

$.ajax({
    url: '/signup',
    type: 'POST',
    data: params,
    success: function (res) {
        console.log('res', res);
    },
    error: function (err) {
        console.log('err', err);
    }
});

服务器js:已经在这里使用body-parser

var express = require("express");
var path = require('path');
var app = express();
var mongoose = require('mongoose');
var request = require("request");
var router = require('./app/routes/route.js');
var functions = require('./app/functions/functions.js');
var http = require('http');
var https = require('https');
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
var model = require('./app/model/model.js');

app.use('/', express.static(__dirname + '/public_html'));
app.use('/', router);
app.use(bodyParser.json({limit: '5mb'}));
app.use(bodyParser.urlencoded({ limit: '5mb', extended: false }));

0 个答案:

没有答案