angvee angvee - 3 months ago 33
Node.js Question

get and replace img src value from html data

I have html file and it stored in AWS S3. I already read that html content file with NodeJS AWS-SDK function(getObject) and it works very well then give me a data. The question is, how to get "src" url from that html data? and how to replace it with new url?

this my example code, I run it in cmd windows :

var AWS = require('aws-sdk');

AWS.config.loadFromPath('./config.json');
var s3 = new AWS.S3();
var params = {Bucket: 'myStoreName/content', Key: 'index.html'};

s3.getObject(params, function(err, data) {
if (err) {
console.log(err, err.stack);
}
else {
var html = data.Body.toString();
console.log(html);
}
});


The result from code above is :

<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
<p>This is sample of test</p>
<img src="./myimage.jpg" />
</body>
</html>


All I want just replce src url to be src="cid:unique@kreata.ee". Is there anyone know how to solve it? is there other way? thankyou for any help

Answer

You might want to use some parser for this.

Cheerio is my choice.

var AWS = require('aws-sdk');
var cheerio = require('cheerio');

AWS.config.loadFromPath('./config.json');
var s3 = new AWS.S3();
var params = {Bucket: 'myStoreName/content', Key: 'index.html'};

s3.getObject(params, function(err, data) {
  if (err) {
    console.log(err, err.stack);
  }
  else {
    var $ = cheerio.load( data.Body.toString() );
    $('body').find('img').attr('src', 'SRC_VALUE_TO_SET');
        console.log( $.html() );
  }    
});

Hope that helps.