Operating environment of this tutorial: Windows 7 system, nodejs version 12.19.0, DELL G3 computer.
nodejs plugin
1. Reading and writing of node-xlsx to Excel
Import and export of excel is a problem that occurs in many systems. In NodeJS, it is no exception. Now, we use NodeJS to realize the reading and writing of Excel files.
In NodeJS, we implement the reading and writing of files. We use the third-party tool node-xlsx module. This module also supports the Excel (.xls) format in 2003 and the Excel format in 2007 (.xlsx)
Now, let's take a look at the specific operation of this module
Reading operations to Excel
First, we need to install this module
cnpm install node-xlsx --save
The second step is to import the module and read the Excel file
const xlsx=require('node-xlsx');const DBUtil=require('./utils/');const fs=require('fs');const path=require('path');function readExcel(path){ var excel=(path); return excel;}var obj=readExcel((__dirname,"./files/"));(obj[0].data);
The above code has completed the reading operation of Excel file. At this time, what we read out is an object, and you can see the information in it in the console.
Write operations to Excel
Now, we will demonstrate reading out the information of a table in the database and then saving it to the local computer. The code is as follows
const excel=require('node-xlsx');const fs=require('fs');const path=require('path');const DBUtil=require('./utils/');function writeExcel(){ var conn=(); ("select * from studentinfo",[],(err,result)=>{ if(err){ } else{ var excelArr=[]; var headerRow=[]; for(var i in result[0]){ (i); } (headerRow); for(var i=0;i<;i++){ var temp=[]; for(var j=0;j<;j++){ (result[i][headerRow[j]]); } (temp); } try { var buff=([{name:'Student Information',data:excelArr}]); ((__dirname,"./files/"),buff); ("ok"); } catch (error) { (err); } } }); ();}writeExcel();
Here, we find that writing to excel is a little more troublesome, because here, we need to recombinate the results obtained in the database and regenerate Excel
Thinking: What if the generated Excel file in the Express framework is as follows for users to download and save it locally?
2. Nodemailer sends mail
Nodejs uses many scenarios for sending emails to users. For example, we often see that after a user registers, we will send a registration message to the email address registered by the user. At this time, if we want to complete this function, we need to use a third-party module of nodemailer. The specific usage steps are as follows:
Install the corresponding module
$ cnpm install nodemailer --asve $ yarn add nodemailer
Import modules and complete code
const nodemailer=require('nodemailer');var transport = ({ service:"qq", auth:{ user:"365055754@", pass:"peshapwpokgvcahe" }});var options={ from:"365055754@", to:"lovesnsfi@", subject:"This is a message from a nodemailer", text:"This is a message from a nodemailer"+(new Date()).toLocaleString(), html:"<h2>This is a test email from <u>nodemail</u>··</h2>"};(options,(err,info)=>{ if(err){ (err); } else{ (info); }});
After sending the message successfully
{ accepted: [ 'lovesnsfi@' ], rejected: [], envelopeTime: 221, messageTime: 830, messageSize: 801, response: '250 Ok: queued as ', envelope: { from: '365055754@', to: [ 'lovesnsfi@' ] }, messageId: '<2fbef9f1-1041-fd59-1111-0b987e8d81da@>' }
At this time, as long as the program can complete our sending request in this u place, the above information will be returned. If we do not see the above information, we need to check the information returned by the error in this place.
Note: When configuring the sending server, we can use a third-party server or a built-in server.
Thinking: If you use a template to replace the sent content
const fs=require('fs');const path=require('path');class MailTemplateModel{ constructor(userName,u_id,registerTime,mail){ =userName; this.u_id=u_id; =registerTime; =mail; } toString(){ var str=`Dear${}Hello! Welcome to register as our member,Your account is${this.u_id},Your registration time is:${}。 Please keep your account and password safe,If you have any questions, please send an email to${}! Thanks!I wish you a happy life!`; return str; }}=MailTemplateModel;
The above code encapsulates the content of the email to be sent into an object, and then uses the template syntax to splice the string.
Thinking: We write the content sent by the above email in a separate external txt file, and then implement it through the replacement of the String object. How to implement this function?
3、child_process
You can create child processes and execute shell scripts.
4、node-readbility
A plugin that can turn website content into simple content.
5、connect
In fact, express also uses this plug-in, and you can also write web programs using connect.
6、express-session
This is a plug-in that uses sessions. The default is forever, which is different from tomcat for 30 minutes, so you need to set the timeout yourself.
7. Basic-auth plugin
It is used for the simplest authentication method, generally used on API requests.
8. bcryptjs plugin (there are always errors reported during bcrypt installation)
Used to have hash processing using salting.
9. Reptiles collection:
(1) Crawling of static pages and API data: request+cheerio/jsdom. Request is a request library that can request post and get information. After obtaining html data, use a third-party parsing library to parse it. Cheerio can be done. For js dynamic rendering pages, you can consider using jsdom, but unfortunately, this is synchronized, and it is not a browser after all.
(2) Crawling of dynamically rendered pages
puppeteer: Use chromiun browser, asynchronous requests, which are very efficient, and opens many APIs to operate the browser, which is very convenient.
Nightmare: The API is very convenient to use. It uses the browser in electron. Although it has not been used, it feels like this is not as flexible as puppeteer.
jsdom: Synchronization has made me abandon its use. Same as selenium.
10、
This is a lightweight format parsing library. If you write the format parsing function yourself, you need a few dozen lines of function code, which is very convenient to use.
This is the end of this article about nodejs plug-in and usage. For more information about nodejs plug-in, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!