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
Express.js – res.jsonp() Method
The res.jsonp() method sends the json response with JSONP support. This method is similar to the res.json() method with the only difference being that it provides the JSONP callback support.
Syntax
res.jsonp ( [body] )
Example 1
Create a file with the name "resJsonp.js" and copy the following code snippet. After creating the file, use the command "node resJsonp.js" to run this code as shown in the example below −
// res.jsonp([body]) 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.jsonp({ title: 'Welcome to Tutorials Point !' });
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Output
C:\home\node>> node resJsonp.js Server listening on PORT 3000
Hit the following Endpoint with a GET request: http://localhost:3000/api
{"title":"Welcome to Tutorials Point !"}
Example 2
Let's take a look at one more example.
// res.jsonp([body]) 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
// With the next() middleware
app.use('/api', function(req, res, next) {
res.jsonp({ name: 'Parent Method' });
next();
})
app.get('/api', function(req, res) {
res.end();
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Output
C:\home\node>> node resJsonp.js Server listening on PORT 3000
Hit the following Endpoint with a GET request – http://localhost:3000/api
{"name":"Parent Method"} Advertisements
