Node js Handle Get Post Request

Posted at: March 14, 2019 8:26 PM

How to handle GET and POST requests in Node js

In this chapter we will learn get and post request in node js with express js. We will use express-generator tool for creating a application. And we will use view engine hbs. And also get response in json format.

First of all, we will install express-generator tools globaly. If you have already installed express-generator tool then no need to install.

Intallation express-generator tool


npm install express-generator -g

Create a Application

Create a application using express-generator tool and set view engine hbs


express --view=hbs myapp

Then install dependencies


cd myapp
npm install

GET Request

Create a get request with route path /student/:id/course/:cid in file routes/index.js


router.get('/student/:id/course/:cid', function(req, res, next) {
  res.render('student', {result: req.params});
  //res.json(req.params);
});

In above code passed two parameters id and cid. You can access parameter using req.params.myParamName for example req.params.id or req.params.cid

Route path: /student/:id/course/:cid
Request URL: http://localhost:3000/student/5/course/10

POST Request

Create a post request with route path /student/submit in file routes/index.js.

You can access field using req.body.fieldname. For example req.body.studentId


router.post('/student/submit', function(req, res, next) {
  var id = req.body.studentId;
  var cid = req.body.courseId;
  res.redirect('/student/' + id + '/course/' + cid);
  //res.json(req.body);
});

Complete code of index.js

routes/index.js


var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express Framework' });
});

router.get('/student/:id/course/:cid', function(req, res, next) {
  res.render('student', {result: req.params});
  //res.json(req.params);
});

router.post('/student/submit', function(req, res, next) {
  var id = req.body.studentId;
  var cid = req.body.courseId;
  res.redirect('/student/' + id + '/course/' + cid);
  //res.json(req.body);
});

module.exports = router;

Create view file views/student.hbs


<h1>Example of get and post requests</h1>
<p>Student Id: {{result.id}}</p>
<p>Course Id: {{result.cid}}</p>

Edit view file views/index.hbs for create form.


<h1>{{title}}</h1>
<p>Welcome to {{title}}</p>

<form action="/student/submit" method="post">
  <input type="text" name="studentId" />
  <input type="text" name="courseId" />
  <button type="submit">Submit</button>
</form>

Run the app with this command


npm start

Then load http://localhost:3000/ in your browser to access the app.

Conclusion

In this lesson we have learned installation of express-generator tool and create application using express-generator tool. And also learn get and post requests.

This lesson also available on YouTube
express-generator tool get request post request

Please leave comments

2 Comments


Silvergate 2 years ago
Hi
Reply
    Silvergate@gmail.com 2 years ago
    Yeees
    Reply