“This article is participating in the technical topic essay node.js advanced road, click to see details”

The writer Lao two words

I’ve been working on the front end, but on the Java back end. The donkey knows to roll up, the author also does not know what to roll up ~ to roll node again. (Just to enjoy the activity, whisper)

Small achievement display

Command line interactive translation

Node knowledge class

Node is a JavaScript server, like Tomcat running Java on the back end. (As if the backend metaphor is not correct, it’s like Nginx. — Self-understanding from the back end

Traditional JS introduction

Js joins the Node agent

Node installation class

Node official download, select stable version download and install. Enter node -v in the CMD command line to check whether the version is displayed. (The installation is successful.)

node -v
Copy the code

Success ~

Node use classroom

【Demo】 Hello, node!

Find a directory folder (in this case, node-test), create hello.js, and write to console.log(” Hello, node! “). )

console.log("Hello, node!")
Copy the code

Enter on the command linenode hello.js, feedback outputHello, node!

Complete process diagram

[Demo] Command line interactive translation

Start by finding a component that can request the API interface, in this case axios. On the command line, type NPM install axios –save

npm install axios --save
Copy the code

Use the translation API interface (calf translation), official tutorial:Get Apikey

Also refer to the API tutorial I wrote in another article to call the calf translation:Juejin. Cn/post / 703267…

Create the translate.js file in which you write more than 20 lines of code

/ / the south - the nuggets at https://juejin.cn/user/2840793779295133
const axios = require('axios');
The argv property returns an array.
// The first argument is the absolute execution path of Node.exe
// The second argument is the absolute path to the currently executing JS file
// The third parameter is the text we want to translate
const argv = process.argv
// return;
if (argv.length < 3) {
    console.log("Need to enter the parameter node x.js text parameter");
    return;
}
const apikey = "8eadabxxxxxxxxxxxxxxxc80c6"; // Here is the apikey I changed, find your own apikey to ~
const srcText = encodeURI(argv[2]); // The string needs to be encoded, otherwise the translation Api will report an error
console.log("**** begin translation ****");
axios.get('http://api.niutrans.com/NiuTransServer/translation?from=zh&to=en&apikey=' + apikey + '&src_text=' + srcText)
    .then(response= > {
        // if (response)
        // console.log(response.data);
        if (response.data.error_msg) {
            console.log("**** translation failure ****");
            console.log(Error message: + response.data.error_msg);
        } else {
            console.log("**** translated successfully ****");
            console.log(The translation result is: + response.data.tgt_text);
        }
        console.log("**** translation end ****");
        // if(response.data)
    })
    .catch(error= > {
        console.log(error);
    });
/ / the south - the nuggets at https://juejin.cn/user/2840793779295133
Copy the code

“2”Command windowInput orderHello, southerner

“3”The illustration Node instruction input with the parameter, in the code has a comment, for reference only ~

Iv. Node class is over

There are many other advantages of Node that I won’t cover here. My personal opinion is: an evolved version of JavaScript. Finally, take your time, boy! That’s the end of this lesson. Class dismissed

Article tail

If you have different views, you are welcome to leave a message or comment below the article. If it is helpful to you, or you agree with it, welcome to give a small thumbs-up, support ~ I am a southerner, a love of computer and love the motherland. (The content of this article is only for learning reference, if there is infringement, I am very sorry, please contact the author immediately delete.)