1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-11 22:42:42 +03:00
Files
node-redis/examples/file.js
Ruben Bridgewater b2613b2270 test fixup
2017-05-06 08:16:19 +02:00

39 lines
1.2 KiB
JavaScript

'use strict'
// Read a file from disk, store it in Redis, then read it back from Redis.
const redis = require('redis')
const client = redis.createClient({
returnBuffers: true
})
const fs = require('fs')
const assert = require('assert')
const filename = 'grumpyCat.jpg'
// Get the file I use for testing like this:
// curl http://media4.popsugar-assets.com/files/2014/08/08/878/n/1922507/caef16ec354ca23b_thumb_temp_cover_file32304521407524949.xxxlarge/i/Funny-Cat-GIFs.jpg -o grumpyCat.jpg
// or just use your own file.
// Read a file from fs, store it in Redis, get it back from Redis, write it back to fs.
fs.readFile(filename, (err, data) => {
if (err) throw err
console.log(`Read ${data.length} bytes from filesystem.`)
client.set(filename, data, console.log) // set entire file
client.get(filename, (err, reply) => { // get entire file
if (err) {
console.log(`Get error: ${err}`)
} else {
assert.strictEqual(data.inspect(), reply.inspect())
fs.writeFile(`duplicate_${filename}`, reply, (err) => {
if (err) {
console.log(`Error on write: ${err}`)
} else {
console.log('File written.')
}
client.end()
})
}
})
})