I'm trying to print name and job of people after taking input from via a html form on the browser. The program also allows deletion and update of record through simple links on the same page. The program is, however, not working.
I even tried creating a "newapp" database (created in the server file) and filled it with some dummy values, the program still wont work!
Following is my node.js file
var express= require("express"),
http= require("http"),
bodyparser= require('body-parser'),
mongo= require("mongodb");
var app= express(),
db= new mongo.Db("newapp",
new mongo.Server("localhost", 27017),
{safe:true}, {auto_reconnect: true}),
people= db.collection("people");
app.use(bodyparser.urlencoded({extended: true}));
app.get("/", function(req, res){
people.find().toArray(function(err, docs){
if(err)
throw err;
res.render("index.jade", {people: docs});
});
});
app.post("/", function(req, res){
people.insert({name: req.body.name, job: req.body.job},
function(err, doc){
if(err)
throw err;
res.redirect("/");
});
});
app.listen(3000);
My index.jade file is:
form(method="POST")
p Name:
input(type="text", name="name")
p Job:
input(type="text", name="job")
p: button Add
if(typeof(people)!=="undefined")
ul
each person in people
li
h2= person.name+ " ("+ person.job+ ")"
p
a(href="/update/#{person._id}") Update
a(href="/delete/#{person._id}") Delete
else
p No People
Any help is much appreciated. Thanks a lot!