Express.js Registration form data 2 Server

Open Command prompt and Go to your Project folder and run these commands 


 npm install --save pug

npm install --save body-parser

npm install --save multer



create index.js file in your project folder from Visual studio code with following code

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);

create views folder in your project folder

create form.pug in views folder

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


open command prompt and goto your project folder and run

node index.js

open browser and type
http://localhost:3000/ to test your application

Comments

Popular posts from this blog

AIDS Meanstack Sample questions