You will certainly add the node. js code snippet _ node. js to your favorites

Source: Internet
Author: User
Nodejs code snippets worth collecting. You can add them to your favorites and use them in subsequent work, if you are interested, you can refer to the following code snippets worth collecting for your reference:

1. For details about node. js static resource compression and caching, refer to: node. js creates a static resource server and uploads files to learn Express. the gzip compression of dynamic files has not been found for a long time, it's just that simple ......
Add the compression module to app. js:
Install it first; var compress = require ('compression'); app. use (compress (); dynamic files can also be compressed by gzip if OK;

2. Capture pictures of sisters:

// Dependent module var fs = require ('fs'); var request = require ("request"); var cheerio = require ("cheerio "); var mkdirp = require ('mkdirp '); // target url var url = 'HTTP: // me2-sex.lofter.com/tag/female photography? Page = '; // local storage directory var dir = '. /images '; // create the directory mkdirp (dir, function (err) {if (err) {console. log (err) ;}}); // send the request (url, function (error, response, body) {if (! Error & response. statusCode == 200) {var $ = cheerio. load (body); $ ('. img '). each (function () {var src = $ (this ). attr ('src'); console. log ('Download' + src); download (src, dir, Math. floor (Math. random () * 100000) + src. substr (-4, 4); console. log ('Download completed ') ;}}}); // download method var download = function (url, dir, filename) {request. head (url, function (err, res, body) {request (url ). pipe (fs. createWriteStream (dir + "/" + filename ));});};

3. decompress the file

var fs = require('fs'),  unzip = require('unzip'); //fs.createReadStream('./angular-swipe-master.zip').pipe(unzip.Extract({ path: './' }));var extract = unzip.Extract({ path: './' }); extract.on('error', function(err) {  console.log(err); }); extract.on('finish', function() {  console.log("unziped!!"); }); fs.createReadStream('./angular-swipe-master.zip').pipe(extract);

4. File compression

var fs = require("fs");var zip = require("node-native-zip");  var archive = new zip();  archive.addFiles([  { name: "app.j", path: "./app.js?1.1.9" }, { name: "package.json", path: "./package.json" }], function (err) { if (err) return console.log(err);   var buff = archive.toBuffer();   fs.writeFile("./test2.zip", buff, function () {  console.log("ziped"); });});

The above is all the content of this article. I hope it will help you learn and make progress together in the new year!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.