神刀安全网

Master Node.js for designers

Master Node.js for designers
Tae Won Ha shows you how to get to grips with Node.js

Have you seen your developer friends scripting like magicians on Terminal, and wished you could do so yourself? Using Node.js, and with a basic knowledge of JavaScript, you can become a script magician too. Node.js liberated JavaScript from the browser. With it, it became possible to write server-side applications and to use JavaScript for scripting – or for almost anything, for that matter.

In this article, I will explore a few typical use cases for Node.js, to help you boost your productivity in your day-to-day work. Let’s get started. First install Node.js using the appropriate installer:

  • OS X: Official installer (from nodejs.org) or Homebrew
  • Linux: Package manager of your distribution
  • Windows: Official installer (from nodejs.org)

All the scripts from this article can be found at netm.ag/nodegit-277 . We will be using the arrow function notation ( netm.ag/arrow-277 ) for brevity.

Master Node.js for designers
Npm is the package manager for JavaScript, and thus for Node.js. Use it to install a huge range of ready-made modules

Installing Modules

All the features of Node.js that go beyond plain JavaScript are found in modules. There are built-
in modules (the standard API), which provide
 basic functionalities like file system operations
 or networking. It’s easy to write your own modules, and there are a vast number of third-party modules ready for use.

The module manager for Node.js is called npm. You can browse available modules, all of which are open source, at npmjs.com . To use those modules, we create a file named package.json and list the module names and their versions. For example, to use lodash and express , we write:

 { "dependencies": { "lodash": "^3.10.1", "express": "^4.13.0" } } 

The syntax for the version follows the semantic version syntax. Here, ^ means the highest version compatible with the specified version.

To actually install the dependencies, we execute the following code in the folder where package.json is located:

 $ npm install 

The easiest way to register third-party dependencies is to let npm do it. For example:

 $ npm install --save lodash 

The --save option lets npm write an entry in package.json and it uses the newest version of the module. The files of the installed modules are stored in the node_modules folder.

At the time of writing, there are more than 210,000 modules available. In many cases, the most difficult part of writing a script is finding out which modules are best suited to what you want to do.

Processing Text Files

The task that crops up most often when scripting is text file processing, the most basic operation being reading. Let’s see how it is done with Node.js.

Node.js has the fs file system module in its standard library. To use it, we have to first require the module:

 var fs = require("fs"); 

Now we can use all functions the fs module has to offer through the variable fs (the list of available functions can be found at nodejs.org/api/fs.html).
To read the content of a file encoded in UTF-8:

 var contentAsString = fs.readFileSync("/Users/a_user/some/ folder/textfile.txt", "utf8"); 

In many cases you’ll want to extract information from one or more files. As a real-life example, let’s look at how the number of words of a net magazine article in the markdown format can be computed. The magazine has the following rule for articles: for layout purposes, each line of code (i.e. each line in the <pre> block), regardless of its length, counts as 10 words. We can use a bookmarklet written by Josh Emerson ( netm.ag/gistlet-277 ) to create a script that does the job for markdown files.

First, install the cheerio and marked modules:

 $ npm install --save cheerio marked 

Then create netmag-count.js with the content listed below:

 var fs = require("fs");
 var cheerio = require("cheerio");
 var marked = require("marked"); 
var fileName = process.argv[2];
 var htmlContent = marked(fs.readFileSync(fileName, "utf8")); var $ = cheerio.load("<body>" + htmlContent + "</body>"); var countWord = str => str.replace(//n/g, " ").split(" ").filter(element => element).length; 
var wordCount = countWord($("body").text());
 var codeLines = 0; $("pre").each(function () {
 var code = $(this).text().replace(/^/n/, "").replace(//n$/, ""); codeLines += code.split("/n").length;
 wordCount -= countWord(code); });
 console.log("%d words (non-code) + %d lines of code = %d total", wordCount, codeLines, wordCount + (codeLines * 10)); 

To use it, specify a markdown file to process:

 $ node netmag-count.js my_article.md
 468 words (non-code) + 54 lines of code = 1008 total 

Now let’s go through the script. First we require the following modules:

To obtain the argument ( my_article.md in the example above), we use Node.js’ built-in array process.argv . The first element of this array is the executing program. The second element is the script being executed. From the third element onwards (thus the index 2 ), the arguments we provide to the script are stored.

The script does the following things:

  • Reads the markdown file
  • 
Converts it to HTML using marked

  • Uses the bookmarklet logic to count the words

Another use case I find helpful is reformatting CSV files. CSV files downloaded from a bank’s website have many unnecessary columns and are horribly formatted. Using the csv module ( github. com/wdavidw/node-csv ), it is almost a trivial task to convert them to a more useful format.

Master Node.js for designers
The number of packages available through npm is growing rapidly, as this graph of data retrieved from modulecounts.com indicates

We won’t discuss this in detail here, but you can find worked examples in the GitHub repo accompanying the article ( netm.ag/nodegit-277 ).

Processing Images

If you are working with images, you often need to batch process files. Using the gm module ( github. com/aheckmann/gm ), it’s easy to automate recurring tasks. To use the gm module, we first have to install GraphicsMagick ( graphicsmagick.org ), which it uses as a backend. This can be done via Homebrew on OS X and via package managers on Linux.


Let’s see how we can remove the EXIF data from all JPEG images in a folder. Install the gm module:

 $ npm install --save gm 

And create remove_exif.js :

 var fs = require("fs"); var path = require("path"); var gm = require("gm"); var dir = process.argv[2]; fs.readdirSync(dir).filter(file => path.extname(file) === ".jpg"). forEach(file => { var originalPath = path.join(dir, file); var newPath = path.join(dir, path.basename(file, path. extname(file)) + ".exif_removed.jpg"); gm(originalPath).noProfile().write(newPath, err => { if (err) throw err; }) }); 

Now, to remove the EXIF data from all JPEG files in a folder:

 $ node remove_exif.js folder/containing/images 

Using the fs module, we can obtain the names of all the files in a folder, and omit all non-JPEG files. We use functions of the path module, part of the standard API, for file path and name manipulation. Finally, we use gm ‘s noProfile() function and write the result to imagename.exif_removed.jpg .

Master Node.js for designers
Npm modules aren’t just for Node.js – you can use them also in browsers for frontend apps. Browserify helps simplify this process

There are many other things that can be done with the gm module, so it’s worth exploring its documentation, which can be found at aheckmann. github.io/gm/docs.html .

Building A Web App

The main use case for Node.js in the industry is
 to build server-side web apps. However, even if you’re building frontend apps, it can still be very useful. We can quickly build a mock backend against which we develop the frontend. Afterwards, we can integrate the real backend.

Mock backends are also very useful for writing browser tests – for example using Protractor. Protractor is designed for AngularJS, but it can be used for non-Angular apps as well.

We will use express , one of the most popular web frameworks for Node.js. Install the express and body-parser modules and create server.js :

 var express = require("express"); 
var jsonParser = require("body-parser").json(); var app = express();
 var server = app .get("/", (req, res) => res.json({msg: "Hello, World!"})) .post("/users", jsonParser, (req, res) => { if (req.body.username === "tae") {
 return res.status(409).json({error: "The Username " + req.body.username + " already exists."}); } return res.status(201).json({"username": req.body. username}); // Let’s pretend to have created a new user })
 .get("/users/:username", (req, res) => { if (req.params.username === "tae") {
 return res.status(200).json({username: "tae", displayName: "Tae Won"}); } return res.status(404).end(); }) .use("/static", express.static(__dirname + "/public")) .listen(4000, () => console.log("Listening at http:// localhost:%s", server.address().port)); 

When you run the web app …

 $ node server.js 

… the server is available under http:/localhost:4000 . You can verify the responses by using Postman or any other REST client.

As you can see, the express instance app has get() and post() (and other http methods, too: netm.ag/httpmethods-277 ). The calls can be also chained. When we want to POST a JSON body, we can use the body-parser plugin (or middleware
in express parlance) to convert the raw body to a JavaScript object, which will be available under req.body for further processing.

The responses can be modified by the res object, specifying the http status code with status() and,
for example, a JSON body with json() . There are many other ways to customise the response, discussed in detail in the online documentation for the res object ( netm.ag/response-277 ).

Often, we want to serve static files. In this case, we can use the express.static() function to specify 
a folder from which to serve the content. In our example, all files in the public folder will be available under http:/localhost:4000/static . There is a lot of middleware available for tasks like these (for an explanation of how to use it, see expressjs.com/en/guide/using-middleware.html ), so we don’t have to write all of the functionality ourselves.

Summary

Node.js opened up the possibility to use JavaScript for non-frontend applications outside the browser. I hope that I have shown you that even if you’re not a developer, by using Node.js for scripting,you can easily boost your productivity. And by using appropriate third-party modules, you can achieve a lot without having to do a lot yourself. Happy scripting!

This article was originally published in net magazine issue 277.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Master Node.js for designers

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址