Tuesday 14 March 2017

How to make a post in nodejs express

I've tried to insert something into my database but it keeps giving me a 404 whenever i submit the form and i can't figure it out. Here's my code:

project.js:

router.post('/posting', function(req, res, next){

var info = {
    'Name': req.body.projectName,
    'Client': req.body.clientName
};

var db = req.db;

db.collection('project').insert(info, function(err, result) {

    if (err) return console.log(err)

    console.log('saved to database')
    res.redirect('/')
})  });

project.ejs:

<form action="/posting" id="new-project"  method="POST" class="form-horizontal hidden">
                    <fieldset>
                        <legend>New Project</legend>
                        <div class="control-group">
                            <label class="control-label" for="input01">Project Name</label>
                            <div class="controls">
                                <input type="text" name="projectName" class="input-xlarge" id="input01" />
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label" for="select01">Client</label>
                            <div class="controls">
                                <input type="text" name="clientName" class="input-xlarge" id="input02" />
                            </div>
                        </div>

                        <div class="form-actions">
                            <button type="submit" id="create-form" class="btn btn-primary">Create</button> <button class="btn">Cancel</button>
                        </div>
                    </fieldset>
                </form>



via Sergiu

No comments:

Post a Comment