使用Express.js app.post请求无效

时间:2015-08-24 03:48:57

标签: javascript node.js express

我遇到Express.js库的问题。

  • 我一直在尝试使用该路线设置基本的发布请求。
  • 我已经为帖子数据安装并使用了body-parser。

如果我专门添加app.post它不起作用,但如果我使用GET或app.all它可以正常使用我的帖子数据。

有人可以看看我的代码,看看我做错了什么?另外,为了进一步澄清,我将稍后拆分所有这些,我只需要数据库连接和一个文件中的其他所有内容来测试。

var express = require('express');
var app = express();
app.set('view engine', 'ejs');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));


var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);
});

var getuser_Information;

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/data/db/');
var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
    // yay!
});
//testing the mogno db for createing a new table schema for a user login using the mognose api example
var UserSchema = mongoose.Schema({
    name:String
});


var usersinfo = mongoose.model('Userinformation', UserSchema);
//sets up the log in


app.post('/',function(req, res) {
    getuser_Information = new usersinfo({ name:req.body.usernames});
    getuser_Information.save(function (err,getuser_Information) {
        if (err) return console.error(err);
        console.log(silence);
    });
    res.render('def',{
        firstnames:getuser_Information
    });
});
<!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
            <title>Bootstrap 101 Template</title>
    
    
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
        </head>
        <body>
        <h1>Hello, world!</h1>
    
        <form class="navbar-form navbar-left" role="search" method="post">
            <div class="form-group">
                <input type="text" class="form-control"  name="usernames">
            </div>
            <button type="submit" class="btn btn-default">Submit</button>
        </form>
    
    
        <h1>Hello <%= firstnames %></h1>
    
    
    
    
        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    
        </body>
        </html>

2 个答案:

答案 0 :(得分:1)

所以我没有路由正确的信息,并弄清楚使用app.all时的差异。这是我现在的代码。

var express = require('express');
var app = express();
app.set('view engine', 'ejs');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));


var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);
});

var getuser_Information;

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/data/db/');
var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
    // yay!
});
//testing the mogno db for createing a new table schema for a user login using the mognose api example
var UserSchema = mongoose.Schema({
    name:String
});


var usersinfo = mongoose.model('Userinformation', UserSchema);
//sets up the log in

app.get('/',function(req, res) {
    res.render('def',{
        firstnames:""
    });
});
app.post('/run',function(req, res) {
    getuser_Information = new usersinfo({ name:req.body.usernames});
    getuser_Information.save(function (err,getuser_Information) {
        if (err) return console.error(err);
        else
            res.render('def',{
                firstnames:getuser_Information
            });
    });


});

process.on('uncaughtException', function (err) {
    console.log("\n\r *Uncaught Exception event* \n\r")
    console.log(err);
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Bootstrap 101 Template</title>


    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<h1>Hello, world!</h1>

<form action="/run" class="navbar-form navbar-left" role="search" method="POST">
    <div class="form-group">
        <input type="text" class="form-control"  name="usernames">
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>


<h1>Hello <%= firstnames %></h1>




<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

</body>
</html>

答案 1 :(得分:0)

app.post('/',function(req, res) {
    getuser_Information = new usersinfo({ name:req.body.usernames});
    getuser_Information.save(function (err,getuser_Information) {
        if (err) return console.error(err);
        else
          res.render('def',{
             firstnames:getuser_Information
          });
    });
});

Node.js是异步的。