1const Apify = require('apify');
2const axios = require('axios');
3
4const fs = require('fs');
5const megabyteFile = fs.readFileSync('./megabyte_image.jpeg');
6
7Apify.main(async () => {
8
9 console.log('Writing to dataset...')
10 await Apify.pushData([{ a: 1 }]);
11 await Apify.pushData([{ a: 1 }, { a: 1 }]);
12 await Apify.pushData([{ a: 1 }, { a: 1 }, { a: 1 }]);
13
14
15
16
17
18
19
20 console.log('Writing to key-value store...')
21 await Apify.setValue('OUTPUT', megabyteFile, { contentType: 'image/jpeg' });
22 await Apify.setValue('OUTPUT', megabyteFile, { contentType: 'image/jpeg' });
23 await Apify.setValue('OUTPUT', megabyteFile, { contentType: 'image/jpeg' });
24
25
26 console.log('Reading from key-value store...')
27 await Apify.getValue('OUTPUT');
28 await Apify.getValue('OUTPUT');
29
30
31 console.log('Reading from dataset...')
32
33 await Apify.utils.sleep(5000);
34 const dataset = await Apify.openDataset();
35 const datasetData = await dataset.getData();
36
37
38 const keyValueStore = await Apify.openKeyValueStore();
39 await keyValueStore.forEachKey(async (key, index, info) => {
40 console.log(`Key at ${index}: ${key} has size ${info.size}`);
41 });
42
43
44 console.log('Writing to request queue...')
45 const queue = await Apify.openRequestQueue();
46 await queue.addRequest({ url: 'http://example.com/aaa' });
47 await queue.addRequest({ url: 'http://example.com/bbb' });
48 await queue.addRequest({ url: 'http://example.com/foo/bar' }, { forefront: true });
49
50
51 console.log('Reading from request queue...')
52 const request = await queue.fetchNextRequest()
53 await queue.getRequest(request.id);
54
55
56 console.log('Doing external network request...')
57 await axios.post('https://example.com', megabyteFile);
58});