wubin ma
wubin ma

Reputation: 11

something about request(url).pipe()

when I use request(url).pip(fs.createWriteStream("1.jpg")), url include some special codes just as "@" and "×", then the NODE.js cannot create the jpg picture, and if I create the http file service, use the url without the special code, I can get the jpg picture correctly. can somebody tell me how to handle these special codes in url? thank you very much!

Upvotes: 1

Views: 1772

Answers (1)

hisener
hisener

Reputation: 1491

You should provide the code but I've tried this code for you.

const fs = require('fs')
const http = require('http')

const request = require('request')

let url = 'https://upload.wikimedia.org/wikipedia/en/8/8a/Text_placeholder_image.jpg' // some jpg

request(url).pipe(fs.createWriteStream('1.jpg'))

http.createServer((req, res) => {
  fs.readFile('1.jpg', (error, content) => {
    if (error) {
      // handle error
      return
    }

    res.writeHead(200, 'image/jpeg')
    res.end(content, 'utf-8')
  })
}).listen(3000)

Then, localhost:3000 worked well.

Used node v6.10.2 and request v2.81.0.

Upvotes: 4

Related Questions