res.app Property in Express.js


The res.app property holds a reference to the instance of an Express Application that is being used by the middleware.

Syntax

res.app

Example 1

Create a file with the name "resApp.js" and copy the following code snippet. After creating the file, use the command "node resApp.js" to run this code as shown in the example below −

// res.app code Demo Example

// Importing the express module
var express = require('express');

// Initializing the express and port number
var app = express();
var PORT = 3000;

// Creating an endpoint
app.get('/', function (req, res) {
   console.log(res.app.get('views'));
   res.end();
});
app.listen(PORT, function(err){
   if (err) console.log(err);
   console.log("Server listening on PORT", PORT);
});

Hit the following URL endpoint for a GET request − http://localhost:3000/

Output

C:\home
ode>> node resApp.js Server listening on PORT 3000 /home/express/test/views

Example 2

Let's take a look at one more example.

// req.range() code Demo Example

// Importing the express module
var express = require('express');

// Initializing the express and port number
var app = express();
var PORT = 3000;

// Creating an endpoint
app.get('/', function (req, res) {
   console.log(req.range());
   res.end();
});
app.listen(PORT, function(err){
   if (err) console.log(err);
   console.log("Server listening on PORT", PORT);
});

Hit the following URL endpoint for a GET request − http://localhost:3000/

Output

C:\home
ode>> node resApp.js settings: { 'x-powered-by': true, etag: 'weak', 'etag fn': [Function: generateETag], env: 'development', 'query parser': 'extended', 'query parser fn': [Function: parseExtendedQueryString], 'subdomain offset': 2, 'trust proxy': false, 'trust proxy fn': [Function: trustNone], view: [Function: View], views: '/home/express/test/views', 'jsonp callback name': 'callback' }, locals: [Object: null prototype] { settings: { 'x-powered-by': true, etag: 'weak', 'etag fn': [Function: generateETag], env: 'development', 'query parser': 'extended', 'query parser fn': [Function: parseExtendedQueryString], 'subdomain offset': 2, 'trust proxy': false, 'trust proxy fn': [Function: trustNone], view: [Function: View], views: '/home/mayankaggarwal/mysql-test/views', 'jsonp callback name': 'callback' } }, mountpath: '/', _router: { [Function: router] params: {}, _params: [], caseSensitive: false, mergeParams: undefined, strict: false, stack: [ [Layer], [Layer], [Layer] ] } }

Updated on: 29-Jan-2022

179 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements