Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
res.location() Method in Express.js
The res.location() method is used for setting the response Location HTTP header to the specified path parameter. Setting the location does not end the process, one can still send some response after setting the location header.
Syntax
res.location( path )
Example 1
Create a file with the name "resLocation.js" and copy the following code snippet. After creating the file, use the command "node resLocation.js" to run this code as shown in the example below −
// res.location(path) 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){
res.location('http://www.tutorialspoint.com');
console.log(res.get('location')); //
http://www.tutorialspoint.com
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\node>> node resLocation.js Server listening on PORT 3000 http://www.tutorialspoint.com
Example 2
Let's take a look at one more example.
// res.location(path) 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){
res.location('/v1/users/profile');
console.log("Location Path is: ", res.get('location')); //
http://www.tutorialspoint.com
res.send(res.get('location'));
});
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\node>> node resLocation.js Server listening on PORT 3000 Location Path is: /v1/users/profile
Advertisements
