Skip to content

Instantly share code, notes, and snippets.

View ganeshkbhat's full-sized avatar

Krishnamurthy G B ganeshkbhat

View GitHub Profile
@ganeshkbhat
ganeshkbhat / expressjs-req.ips-snippet.js
Created October 4, 2018 03:00
ExpressJS Series: Getting the req.ips
app.post("/someUrl", function(req, res) {
 let IPAddress = req.ips;
 res.send(200).send({result: 'Success', tracePaths: IPAddress});
});
@ganeshkbhat
ganeshkbhat / expressjs-req.method-snippet.js
Created October 4, 2018 03:01
ExpressJS Series: Getting the req.method
app.post("/someUrl", function(req, res) {
 res.send(200).send({result: 'Your request method', method: req.method});
});
@ganeshkbhat
ganeshkbhat / expressjs-req.params-snippet.js
Created October 4, 2018 03:01
ExpressJS Series: Getting the req.params
app.post("/someUrl/:categoryId/:someName", function(req, res) {
res.send(200).send({result: 'Your params as array', params: req.params});
});
@ganeshkbhat
ganeshkbhat / expressjs-req.baseUrl-req.originalUrl-req.path-difference.snippet.js
Created October 4, 2018 03:03
ExpressJS Series: Difference the req.baseUrl req.originalUrl req.path
app.use('/admin', function(req, res, next) {
/* GET 'http://www.example.com/admin/new' */
console.log(req.originalUrl); // '/admin/new'
console.log(req.baseUrl); // '/admin'
console.log(req.path); // '/new'
next();
});
@ganeshkbhat
ganeshkbhat / expressjs-req.protocol-snippet.js
Created October 4, 2018 03:05
ExpressJS Series: Getting the req.protocol
app.post("/someUrl", function(req, res) {
let protocol = req.protocol;
if (protocol !== 'https') {
res.status(426).send("Protocol Insecure");
}
res.send(200).send({result: 'Success', protocol: protocol});
});
@ganeshkbhat
ganeshkbhat / expressjs-req.query-snippet.js
Created October 4, 2018 03:05
ExpressJS Series: Getting the req.query
app.post("/someUrl/:categoryId/:someName", function(req, res) {
// Request to /someUrl/12/gary?username=ganesh&password=shouldnotbehere
res.send(200).send({result: 'Query params as object', queryParams: req.query});
});
@ganeshkbhat
ganeshkbhat / expressjs-req.route-snippet.js
Created October 4, 2018 03:06
ExpressJS Series: Getting the req.route
app.get('/user/:id?', function userIdHandler(req, res) {
console.log(req.route);
res.send('GET');
});
@ganeshkbhat
ganeshkbhat / expressjs-req.secure-snippet.js
Created October 4, 2018 03:07
ExpressJS Series: Getting the req.secure
app.post("/someUrl", function(req, res) {
let protocol = req.secure;
if (!protocol) {
res.status(426).send("Protocol Insecure");
}
res.send(200).send({result: 'Success', protocol: protocol});
});
@ganeshkbhat
ganeshkbhat / expressjs-req.accepts-snippet.js
Created October 4, 2018 03:08
ExpressJS Series: Usage req.accepts
app.post("/someUrl", function(req, res) {
// Accept => "text/html" (Header Accept in request)
let returnContent = req.accepts(['json', 'text']);
// returns => 'json'
if (!returnContent) {
res.status(406).send("Not Acceptable");
}
res.send(200).send({result:"Some Result"});
});
@ganeshkbhat
ganeshkbhat / expressjs-req.header-snippet.js
Created October 4, 2018 03:09
ExpressJS Series: Usage req.header
app.post("/someUrl", function(req, res) {
// Content-Type => "application/json" (Header Content-Type in request)
let getContent = req.header('content-type');
if (getContent !== 'application/json') {
res.status(406).send("Not Acceptable");
}
// Raw buffer capture in request body (will be covered later)
const contentBody = JSON.parse(req.body.toString('utf8'));
res.send(200).send({result:"Success", content: contentBody});
});