Express.js with Mongoose

 Program 1:    first.js

var express = require('express');
var app = express();

app.get('/greeting', function(req, res){

   res.send("Hello world!");
});

app.listen(3000);



npm install --save pug

npm install --save body-parser

npm install --save multer


index.js

var express = require('express');

var bodyParser = require('body-parser');

var multer = require('multer');
var upload = multer();
var app = express();

app.get('/', function(req, res){
   res.render('form');
});

app.set('view engine', 'pug');
app.set('views', './views');

// for parsing application/json
app.use(bodyParser.json());

// for parsing application/xwww-
app.use(bodyParser.urlencoded({ extended: true }));
//form-urlencoded

// for parsing multipart/form-data
app.use(upload.array());
app.use(express.static('public'));

app.post('/', function(req, res){
   console.log(req.body);
   res.send("recieved your request!");
});
app.listen(3000);

form.pug

html
   head
      title Form Tester
   body
      form(action = "/", method = "POST")
         div
            label(for = "say") Say:
            input(name = "say" value = "Hi")
         br
         div
            label(for = "to") To:
            input(name = "to" value = "Express forms")
         br
         button(type = "submit") Send my greetings




npm install --save mongoose@6.0



var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1/my_db');
var personSchema = mongoose.Schema({
   name: String,
   age: Number,
   nationality: String
});
var Person = mongoose.model("Person", personSchema);
person.pug
html
head
   title Person
   body
      form(action = "/person", method = "POST")
      div
         label(for = "name") Name: 
         input(name = "name")
      br
      div
         label(for = "age") Age: 
         input(name = "age")
      br
      div
         label(for = "nationality") Nationality: 
         input(name = "nationality")
      br
      button(type = "submit") Create new person
app.post('/', function(req, res){
   var personInfo = req.body; //Get the parsed information
   
   if(!personInfo.name || !personInfo.age || !personInfo.nationality){
      res.render('show_message', {
         message: "Sorry, you provided worng info", type: "error"});
   } else {
      var newPerson = new Person({
         name: personInfo.name,
         age: personInfo.age,
         nationality: personInfo.nationality
      });
		
      newPerson.save(function(err, Person){
         if(err)
            res.render('show_message', {message: "Database error", type: "error"});
         else
            res.render('show_message', {
               message: "New person added", type: "success", person: personInfo});
      });
   }
});
show_message.pug
html
   head
      title Person
   body
      if(type == "error")
         h3(style = "color:red") #{message}
      else
         h3 New person, 
            name: #{person.name}, 
            age: #{person.age} and 
            nationality: #{person.nationality} added!
form.pug

html
   head
      title Form Tester
   body
      form(action = "/", method = "POST")
         div
            label(for = "say") Name:
            input(name = "name" )
         br
         div
            label(for = "to") Age :
            input(name = "age")
         br
         div
            label(for = "to") Nationality :
            input(name = "nationality")
         br
         button(type = "submit") Add me

Comments

Popular posts from this blog

AIDS Meanstack Sample questions

Express.js Registration form data 2 Server