
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 App Set Method
The app.set() function assigns or sets a setting name to value. This can store any type of value as the user wants, but there are some certain names that can be used to configure the behaviour of the server
Some of the properties that can be configured with the set functionality are −
env
etag
jsonp escape,etc
Syntax
app.set(name, value)
Example 1
Create a file with the name "appSet.js" and copy the following code snippet. After creating the file, use the command "node appSet.js" to run this code.
// app.set() Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); var PORT = 3000; // Setting the value to name app.set('title', 'Welcome to TutorialsPoint'); // Creating an endpoint app.get('/', (req, res) => { res.send(app.get('title')); console.log(app.get('title')); }) app.listen(PORT, function(err){ if (err) console.log(err); console.log("Server listening on PORT", PORT); });
Now, hit the following endpoint/api to call the function
Output
C:\home
ode>> node appSet.js Server listening on PORT 3000 Welcome to TutorialsPoint
Example 2
Let’s take a look at one more example.
// app.set() Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); var PORT = 3000; // Setting the value to name app.set('title', 'Hi, The requested page is not available'); // Creating an endpoint app.get('/*', (req, res) => { res.send(app.get('title')); console.log(app.get('title')); }) app.listen(PORT, function(err){ if (err) console.log(err); console.log("Server listening on PORT", PORT); });
For the above function, you can call any endpoint after '/' and it will be passed through the above function only. For example
http://localhost:3000/,
Output
C:\home
ode>> node appRoute.js Server listening on PORT 3000 Hi, The requested page is not available
Advertisements