Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Express.js – res.get() Method
The res.get() method is used to return the HTTP headers specified by the field. The match is case-insensitive and therefore returns all the matching patterns.
Syntax
res.get( field )
Example 1
Create a file with the name "resGet.js" and copy the following code snippet. After creating the file, use the command "node resGet.js" to run this code as shown in the example below −
// res.get(field) Method Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Defining an endpoint
app.get('/api', function(req, res){
// Setting the Content-type
res.set({
'Content-Type': 'application/json',
});
// "text/plain"
console.log(res.get('Content-Type'));
res.end();
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Hit the following Endpoint with a GET request − localhost:3000/api
Output
C:\home
ode>> node resGet.js Server listening on PORT 3000 application/json; charset=utf-8
Example 2
Let's take a look at one more example.
// res.get(field) Method Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Defining an endpoint
app.use('/api', function(req, res, next){
//Setting the response
res.set({
'Content-Type': 'application/xml',
});
next();
})
app.get('/api', function(req, res){
console.log("Content-Type is: ", res.get('Content-Type'));
res.send();
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Hit the following Endpoint with a GET request −localhost:3000/api
Output
C:\home
ode>> node resGet.js Server listening on PORT 3000 Content-Type is: application/xml
Advertisements