Dipesh Raichana
Dipesh Raichana

Reputation: 1068

nodejs throws cannot post error

I am trying to post the data from html form but it is showing an error "cannot POST".

This is my code:

controller/InquiryDetails.js:

   var mongoose = require('mongoose'),
   InquiryDetails = mongoose.model('InquiryDetails');


   exports.add = function(req, res) {
   InquiryDetails.create(req.body, function (error, details) {
   if (error) return console.log(error); 
   return res.send(details);
  });
  }

routes/Inquiry.js

 var express = require('express');
  var router = express.Router();
 var bodyParser = require('body-parser');
 var app = express();

  app.use(bodyParser.urlencoded({extended:true}));

 app.post('/InquiryDetails', function(req,res,err){
 if(err) console.log(err);
 res.json(req.body);
 console.log(req.body);

});
module.exports = router;

model/InquiryDetails.js

 var mongoose = require('mongoose');
 var Schema = mongoose.Schema;

var myskyll = new Schema({
  name: String, 
  email: String,
  state: String,
  country: String,
  school: String,
  profession: String,
  phone: Number 
});

  mongoose.model('InquiryDetails', myskyll);

app.js:

var express = require('express');
var mongoose = require('mongoose');
var request = require('request');

var connect = require('connect');
var serveStatic = require('serve-static');
connect().use(serveStatic(__dirname+"/index.html")).listen(8080);
var mongoUri = 'mongodb://localhost/myskyll'; 
mongoose.connect(mongoUri);
var db = mongoose.connection;
db.on('error', function () {
throw new Error('unable to connect to database at ' + mongoUri);
});

var app = express();
console.log("connection successfull");
app.configure(function(req,res){
app.use(express.bodyParser());
});

 app.use(express.static(__dirname + "/" ));

 require('./models/InquiryDetails');
 require('./routes/Inquiry');

 app.listen(3000);
 console.log('Listening on port 3000...');

index.html:

   <form method="post" id="form1" action="InquiryDetails">
    Your Name:<span class="imp">*</span><br/><input type="text"    
     name="name" placeholder="Firstname Lastname" class="textbox"   
     autofocus required pattern="[A-Za-z-0-9]+\s[A-Za-z-'0-9]+"   
     title="'Firstname' <space> 'Lastname'"/> <br/><br/>

    Email:<span class="imp">*</span><br/><input 
     type="email" name="email" placeholder="Email" class="textbox"  
       required/><br/><br/>

    city:<span class="imp">*</span><br/><input type="text" name="city"  
    placeholder="city" class="textbox" required/><br/><br/>

                    State/Country:<span class="imp">*</span><br/>  
           <input type="text" name="country"  
         placeholder="State/Country" class="textbox" required /><br/>
       <br/>

        School/Institution:<span class="imp">*</span><br/><input  
     type="text" name="school" placeholder="Your School or  
         Institution" c lass="textbox" required /><br/><br/>

        Your Profession:<br/><input type="text" name="profession"  
   placeholder="Profession" class="textbox"><br/><br/>
    Phone:<br/><input type="text" name="phone" placeholder="Phn. no. 
   with country code" class="textbox" pattern="\+[0-9]+" title="Only  
  digits" /><br/><br/>

<input type="submit" value="Submit"   
     class="btn3"/>&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="reset" value="Clear"  class="btn3" />


    </form>

I have tried in postman API client to post the data but it shows 404 status while posting the data. Do I have any problem in my code? Any suggestion will be appreciated!

Upvotes: 0

Views: 3627

Answers (1)

jfriend00
jfriend00

Reputation: 707696

You are creating multiple app objects. One in app.js and a different one in routes/Inquiry.js.

For this type of application, you will want to be using only one app object that you share with anyone who needs it because only one of them is actually registered with your web server and is being used.

That is likely why your app.post() handler is not working because it's registered on the wrong app object - the one that is not connected to a live server.


There are several other things that look incorrect in the code:

  1. The third argument to an app.post() callback is a next() function reference, not the err that you have declared and are testing.

  2. You are doing require('./models/InquiryDetails'); and that module exports some things, but you are not assigning the result of the require to anything so those exports are unusable.

  3. You are initializing the connect module, but if you are using a recent version of Express that module is no longer needed as the functionality it used to provide for Express is now built into Express.

Upvotes: 1

Related Questions