Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const http = require('http');
- const API = '';
- class Lambda {
- constructor() {
- this.get(
- `${API}/categories`,
- (data) => {
- console.log('First category is', data.categories[0]);
- });
- }
- get(url, callback, error) {
- const request = http.get(
- url,
- (response) => {
- response.setEncoding('utf8');
- let body = '';
- response.on('data', (chunk) => {
- body += chunk;
- });
- response.on('end', () => {
- body = JSON.parse(body);
- callback(body);
- });
- }
- );
- request.on('error', error);
- }
- post(url, data, callback, error) {
- const options = {
- hostname: 'www.someapi.com',
- port: 80,
- path: '/seturl',
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'Content-Length': Buffer.byteLength(JSON.stringify(data)),
- }
- };
- const request = http.request(
- options,
- (response) => {
- response.setEncoding('utf8');
- let body = '';
- response.on('data', (chunk) => {
- body += chunk;
- });
- response.on('end', () => {
- body = JSON.parse(body);
- callback(body);
- });
- }
- );
- request.write(data);
- request.end();
- }
- }
- module.exports = new Lambda();
Add Comment
Please, Sign In to add comment