3. Node.js APIs and Web Scraping
Activity 4 Scraping Products and Prices from Storefront
Solution
- Start the dynamic server to serve the storefront application using the code from Exercise 14, Serving Dynamic Content, in this chapter:
$ node Lesson03/Activity04/ Static resources from /path/to/repo/Lesson03/Activity04/static Loaded 21 products... Go to: http://localhost:3000
- In a new Terminal, create a new
npm
package, installjsdom
, and create theindex.js
entry file:$ npm init ... $ npm install jsdom + jsdom@15.1.1 added 97 packages from 126 contributors and audited 140 packages in 12.278s found 0 vulnerabilities
- Call the
require()
method to load all the modules you will need in the project:const fs = require('fs'); const http = require('http'); const JSDOM = require('jsdom').JSDOM;
- Make an HTTP request to
http://localhost:3000
:const page = 'http://localhost:3000'; console.log(`Downloading ${page}...`); const request = http...