Studying NodeJs HTTP Requests

--- title: "Studying NodeJs HTTP Requests" date: 2018-03-19T11:19:06+03:00 draft: false description: "" tags: categories: js type: post url: author: "Mert Nuhoglu" output: html_document blog: mertnuhoglu.com resource_files: - ex/study_http_requests_in_nodejs/src/index.js - ex/study_http_requests_in_nodejs/src/index02.js path: ~/projects/study/js/study_http_requests_in_nodejs.Rmd ---

v01: Basics

Source code in https://github.com/mertnuhoglu/study/js/ex/study_http_requests_in_nodejs/

HTML page:

mkdir -p ex/study_http_requests_in_nodejs && $(cd $_ && npm init -y && pnpm i parcel-bundler request)
mkdir -p ex/study_http_requests_in_nodejs/src && touch ex/study_http_requests_in_nodejs/src/index.js

Edit ex/study_http_requests_in_nodejs/src/index.js:

const request = require('request');
request('http://jsonplaceholder.typicode.com/users/1', function (error, response, body) {
  console.log('error:', error);
  console.log('statusCode:', response && response.statusCode);
  console.log('body:', body);
});
node ex/study_http_requests_in_nodejs/src/index.js | sed -n '1,6 p'
## module.js:545
##     throw err;
##     ^
## 
## Error: Cannot find module 'request'
##     at Function.Module._resolveFilename (module.js:543:15)
##     at Function.Module._load (module.js:470:25)
##     at Module.require (module.js:593:17)
##     at require (internal/module.js:11:18)
##     at Object.<anonymous> (/Users/mertnuhoglu/projects/jekyll/mertnuhoglu.com/content/tech/ex/study_http_requests_in_nodejs/src/index.js:1:79)
##     at Module._compile (module.js:649:30)
##     at Object.Module._extensions..js (module.js:660:10)
##     at Module.load (module.js:561:32)
##     at tryModuleLoad (module.js:501:12)
##     at Function.Module._load (module.js:493:3)

v02: Use ES6 Promise

cd ex/study_http_requests_in_nodejs && pnpm i --save request-promise-native && touch ex/study_http_requests_in_nodejs/src/index02.js
var request = require('request-promise-native');
request('http://jsonplaceholder.typicode.com/users/1')
  .then( html => console.log('body:', html) )
  .catch( err => console.log('error:', err) );
node ex/study_http_requests_in_nodejs/src/index02.js | sed -n '1,6 p'
## module.js:545
##     throw err;
##     ^
## 
## Error: Cannot find module 'request-promise-native'
##     at Function.Module._resolveFilename (module.js:543:15)
##     at Function.Module._load (module.js:470:25)
##     at Module.require (module.js:593:17)
##     at require (internal/module.js:11:18)
##     at Object.<anonymous> (/Users/mertnuhoglu/projects/jekyll/mertnuhoglu.com/content/tech/ex/study_http_requests_in_nodejs/src/index02.js:1:77)
##     at Module._compile (module.js:649:30)
##     at Object.Module._extensions..js (module.js:660:10)
##     at Module.load (module.js:561:32)
##     at tryModuleLoad (module.js:501:12)
##     at Function.Module._load (module.js:493:3)

 Tech    19 Mar, 2018

Any work (images, writings, presentations, ideas or whatever) which I own is always provided under
Creative Commons License Creative Commons Attribution-Share Alike 3.0 License

Mert Nuhoglu is a Trabzon-born programmer and data scientist.

You may also like...