Israel Martins
Israel Martins

Reputation: 318

How to create a web proxy in nodejs using phantomjs or alternative browser?

I've been trying to create a web proxy server with phanthonjs or similar and view and navigate in the browser

var phantom = require('phantom');

phantom.create().then(function(ph) {
  ph.createPage().then(function(page) {
    page.open('https://stackoverflow.com/').then(function(status) {
      console.log(status);
      page.property('content').then(function(content) {
        console.log(content);
        page.close();
        ph.exit();
      });
    });
  });
});

Upvotes: 2

Views: 355

Answers (1)

paulobunga
paulobunga

Reputation: 385

const express = require('request');
const puppeteer = require('puppeteer');

const app = express();

app.use('/', async (req, res) => {
   const url = 'http://somesite.com';
   const browser = await puppeteer.launch();
   const page = await browser.newPage();
   await page.goto(url);
   const content = await page.content();
   res.send(content);
   await browser.close();
});

app.listen(3000, () => { console.log('App is running on port 3000') }

Thats how I would implement it, If i wanted to use a headless browser. Syntax differs with other headless browsers. But Idea is quite the same. :)

Upvotes: 2

Related Questions