繁体   English   中英

使用Express.js app.post请求不起作用

[英]Using the Express.js app.post request is not working

我在Express.js库中遇到问题。

  • 我一直在尝试通过路线设置基本的发帖请求。
  • 我已经安装了body解析器并将其用于发布数据。

如果我专门添加了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> 

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM