SierraSoftworks / Iridium

A high performance MongoDB ORM for Node.js
http://sierrasoftworks.github.io/Iridium/
Other
570 stars 25 forks source link

Getting a handle on the request and response objects in Typescript #112

Closed SreekarReddyAparacherla closed 6 years ago

SreekarReddyAparacherla commented 6 years ago

Hello! I am trying to implement backend support for recaptcha for my website, currently running on localhost. The boilerplate code I have is for Javascript and looks as follows:

const express = require('express'); const bodyparse = require('body-parser'); const request = require('request'); const app = express(); app.post('/subscribe', (req, res) => { this.log.debug('here boss'); if ( req.body.captcha === undefined || req.body.captcha === '' || req.body.captcha === null ) { return res.json({success: false, msg: 'please select captcha'}); } // Secret key const secretKey = '6LcdDkEUAAAAAJUFuKVrT1-UYgrNOSvO-vAgw9ci'; // Verify URL const baseVerifyUrl = 'https://google.com/recaptcha/api/siteverify'; const queryVerifyUrl = ?secret=${secretKey}&response=${req.body.captcha}&remoteip=${req.connection.remoteAddress}; const verifyUrl = baseVerifyUrl + queryVerifyUrl; // Maker request ot verify URL request(verifyUrl, (err, response, body) => { body = JSON.parse(body); // If not successful if (body.success !== undefined && !body.success) { return res.json({success: false, msg: 'Failed captcha verification'}); } // If successful return res.json({success: true, msg: 'captcha passed'}); }); }); return this.memberService.create(param); }

As you can see, the use of express, request and body-parser lets me get a handle on the request object when I use Javascript. Similarly, how do I get a handle on the request object when using Typescript?