Alex Gwartney
Alex Gwartney

Reputation: 87

Using the Express.js app.post request is not working

I am having an issue with the Express.js library.

If I specifically add app.post it does not work but if I use GET or app.all it works just fine with my post data.

Could some one take a look at my code and see what i could be doing wrong? Also just for a further clarification i will be splitting all this up later on i just have the data base connection and every thing else in one file to test things.

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>

Upvotes: 1

Views: 3622

Answers (2)

Alex Gwartney
Alex Gwartney

Reputation: 87

So i was not routing the correct information and figured out the diffrences of when using app.all. here is the code that i have now.

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>

Upvotes: 1

Vishnu
Vishnu

Reputation: 12283

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 is asynchronous.

Upvotes: 0

Related Questions