使用Expressjs app.post请求不起作用?

我有一个expressjs库的问题。 我一直试图build立一个基本的路线请求。 我已经安装和正在使用身体分析器的发布数据。 但是,当我特别是广告app.post它不起作用。 但是当我使用get或app.all时,它对我的​​发布数据来说工作得很好。 有人可以看看我的代码,看看我可能做错了什么? 也只是为了进一步的澄清,我将分裂所有这些后来我只是有一个文件的数据库连接和其他东西来testing的东西。

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> 

所以我没有路由正确的信息,并找出使用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> 
 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是asynchronous的。