NodeJS表达Post表单 - 操作总是404

时间:2016-03-23 14:29:38

标签: javascript node.js post express action

无论我在表单的操作中声明什么链接,它总是返回404错误。如果我改变方法得到,它就不会。 我想使用post,因为我试图从输入字段中获取数据并将其发布到我的mongedb数据库中:

HTML:

<form class="container-fluid searchContainer" action="/active" method="post">
    <div class="col-xs-6">
        <div class="form-group">
            <label for="businessUnit">Business unit</label>
            <input type="text" ng-model="businessUnit" id="businessUnit" name="businessUnit" class="form-control"/>
        </div>
        <div class="form-group">
            <label for="tenderId">Tender ID</label>
            <input type="text" name="tenderID" ng-model="tenderId" id="tenderId" class="form-control"/>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="tenderDate">Tender Bidding Date</label>
            <input type="date" ng-model="tenderDate" name="tenderDate" id="tenderDate" class="form-control"/>
        </div>
        <div class="form-group">
            <label for="therapyArea">Therapy area</label>
            <input type="text" ng-model="therapyArea" name="therapyArea" id="therapyArea" class="form-control"/>
        </div>

    </div>
    <div class="col-md-12 text-right">
        <input type="submit" id="btnCreate" value="Create" class="btn btn-primary" >
    </div>
</form>

JAVASCRIPT:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var active = require('./routes/active');



var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);
app.use('/active', active);


// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers



// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});


module.exports = app;


var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/tenders');

var db = mongoose.connection;

db.on('error', function(err) {
    console.log('connection error', err);
});
db.once('open', function() {
    console.log('connected.');
});


var Schema = new mongoose.Schema({
    TenderID: String,
    TenderDate: String,
    BusinessUnit: String,
    TherapyUnit: String
});



var Tender = mongoose.model('Tender', Schema);


app.post('/active', function(req, res) {

    new Tender({
        TenderID: req.body.tenderID,
        TenderDate: req.body.tenderDate,
        BusinessUnit: req.body.businessunit,
        TherapyUnit: req.body.therapyUnit
    }).save(function(err, doc) {
        if (err) res.json(err);
        else res.send('Succesfully insterted')

    })

});

2 个答案:

答案 0 :(得分:3)

因为在定义app.post('/active')之前定义了404路由处理程序,所以express首先评估404规则并返回它。您的POST /活动路由永远不会被评估。要解决此问题,您需要在404路线(应该始终是您的最后路线)之前提供快速您的POST /有效路线。

除非您在文件./routes/active中定义了等效的POST /活动路由,否则行

app.use('/active', active);
即使在404路线之前进行评估,

也无法解决此问题。您可以详细了解app.use() vs app.get|post|etc() in the express docs

Express会按照通过use()get|post|delete|etc()

传递给它的顺序来评估规则

答案 1 :(得分:0)

您已在服务器中定义了get路由。尝试更改

app.get('/active',function(req,res){

app.post('/active',function(req,res){

这样,您将回复浏览器发布的帖子请求。