Reputation: 309
I have a file server.js that looks like so:
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var Bear = require('./models/bear');
mongoose.connect('mongodb://123:[email protected]:53/bears');
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended : false}));
app.use(bodyParser.json());
var port = 8080;
var router = express.Router();
router.use(function(req, res, next){
//console.log("Working....");
next();
});
router.get('/', function (req, res){
res.json({ message: "API Works!"});
});
router.route('/bears')
.post(function(req, res) {
console.log("hi");
var bear = new Bear(req.body); // create a new instance of the Bear model
console.log(bear);
// save the bear and check for errors
bear.save(function(err) {
if (err)
return res.send(err);
res.json({ message: 'Bear created!' });
});
});
app.use("/", (req, res) =>{
res.sendFile(__dirname + "/index.html");
});
app.use('/books', router);
app.listen(port);
console.log("Listening on port "+ port);
And my Index.html file looks like so:
<head>
</head>
<body>
<form method="POST" action="/bears">
Enter Book to add
<input type="text" id="book_name"/>
<br /><br />
Enter Quantity
<input type="number" id="book_quantity"/>
<br /><br />
Enter Price
<input type="text" id="book_price"/>
<br /><br />
<button type="submit"> Add Book </button>
</form>
</body>
I was wondering why it doesn't enter the
router.route('/bears')
.post(function(req, res){});
when clicking the "Add Book" button even thought I've set the
action="/bears"
attribute.
When testing with curl, it works fine, just not with this html form
I test with curl like so:
curl -i -X POST -H "Content-Type: application/json" -d '{ "book_name" : "Android", "book_quantity": 2,"book_price": 580 }' localhost:8080/books
I'm new to Javascript so any help would be appreciated!
Upvotes: 0
Views: 2189
Reputation: 42746
app.use("/", (req, res) =>{
res.sendFile(__dirname + "/index.html");
});
Is being used for all the requests and not just '/. Instead use
app.get()` to handle the index.html page. This should then let your requests go through.
app.get("/", (req, res) =>{
res.sendFile(__dirname + "/index.html");
});
And the endpoint would be /books/bears
and not just /bears
since your route use is app.use('/books',router)
so your endpoints would need to start with /books
<form method="POST" action="/books/bears">
Upvotes: 1
Reputation: 5943
For post call through html form, "name" attributes for all input tags are mandatory
<form method="POST" action="/books/bears">
Enter Book to add
<input type="text" id="book_name" name="book_name"/>
<br /><br />
Enter Quantity
<input type="number" id="book_quantity" name="book_quantity"/>
<br /><br />
Enter Price
<input type="text" id="book_price" name="book_price"/>
<br /><br />
<button type="submit"> Add Book </button>
</form>
Also use
app.use(bodyParser.urlencoded({ extended: true }));
in server code
This is because form sends post data in application/x-www-form-urlencoded format, so access as req.body.book_name in server.
if json format is needed then use javascript on client and make json and then send.
Upvotes: 1