Friday, 26 May 2017

Cannot POST Form in Node

I am trying to post a simple form from a static page to a database and then re-render the page. Right now I am getting a "Cannot Post /Index" error.

Here is my the form code:

<div class="contact-form">
                            <form action="/index" method="post">
                                <div class="col-md-6">
                                    <input type="text" class="form-control" placeholder="Name" name="name">
                                </div>
                                <div class="col-md-6">
                                    <input type="email" class="form-control" placeholder="Email" name="email">
                                </div>
                                <div class="col-md-6">
                                    <input type="tel" class="form-control" placeholder="Phone Number" name="phone">
                                </div>
                                <div class="col-md-6">
                                    <input type="text" class="form-control" placeholder="# of Trucks" name="truckNum">
                                </div>
                                <div class="col-md-12">
                                    <input type="text" class="form-control" placeholder="Company" name="company">
                                </div>
                                <div class="col-md-12">
                                    <input type="text" class="form-control" placeholder="Address" name="address">
                                </div>
                                <div class="col-md-12">
                                    <textarea class="form-control" placeholder="Message" rows="4" name="message"></textarea>
                                </div>
                                <div class="col-md-8">
                                    <input type="submit" class="form-control text-uppercase" value="Send">
                                </div>
                            </form>
                        </div>

Here is the router code:

app.get("/", function(req, res) {
res.sendFile(path.join(__dirname, "/../public/index.html"));

});

app.post("/", function(req,res){
db.Contact.create({
        name: req.body.name,
        email: req.body.email,
        phone: req.body.phone,
        company:req.body.company,
        address:req.body.address,
        truckNumber:req.body.truckNum
    }).then(function(data) {
        res.sendFile(path.join(__dirname, "/../public/index.html"));
    });

}) };



via Patrick Bentley

No comments:

Post a Comment