Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What plug-ins are available for nodejs

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "what plug-ins nodejs has". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Nodejs plug-ins include: "node-xlsx", nodemailer, "node-readbility", connect, "express-session", "basic-auth", bcryptjs, puppeteer, nightmare and so on.

This tutorial operating environment: windows7 system, nodejs version 12.19.0, DELL G3 computer.

Nodejs plug-in

1. Node-xlsx 's reading and writing of Excel

The import and export of excel is a problem in many systems, and there is no exception in NodeJS. Now, we use NodeJS to read and write Excel files.

In NodeJS, we use the third-party tool node-xlsx to read and write files. This module supports both 2003 Excel (.xls) format and 2007 Excel format (.xlsx).

Now, let's take a look at the specific operation of this module.

Read operation to Excel

First, we need to install this module

Cnpm install node-xlsx-save

Second, import the module and read the Excel file

Const xlsx=require ('node-xlsx'); const DBUtil=require ('. / utils/DBUtil.js'); const fs=require ('fs'); const path=require (' path'); function readExcel (path) {var excel=xlsx.parse (path); return excel;} var obj=readExcel (path.join (_ dirname, ". / files/studentinfo.xls"); console.log (obj [0] .data)

The above code has finished reading the Excel file. At this time, what we read out is an object, and we can see the information in the console.

Write operation to Excel

Now, let's demonstrate reading the information from a table in the database and 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/DBUtil.js'); function writeExcel () {var conn=DBUtil.getConn (); conn.query ("select * from studentinfo", [], (err,result) = > {if (err) {} else {var excelArr= []; var headerRow= []; for (var i in result [0]) {headerRow.push (I)) } excelArr.push (headerRow); for (var iTuno console.log I {if (err) {console.log (err);} else {console.log (info);}})

The message after being sent successfully

{accepted: ['lovesnsfi@live.com'], rejected: [], envelopeTime: 221, messageTime: 830, messageSize: 801, response:' 250 Ok: queued as', envelope: {from: '365055754roomq.compositions, to: [' lovesnsfi@live.com']}, messageId:''}

At this time, as long as the program can complete our request in these u places, it will return the above information. If we do not see the above information, we need to check the information returned by error in this place.

Note: when we have to configure the sending server, we can use either a third-party server or a built-in server

Think about it: if you replace the sent content with a template

Const fs=require ('fs'); const path=require (' path'); class MailTemplateModel {constructor (userName,u_id,registerTime,mail) {this.userName=userName; this.u_id=u_id; this.registerTime=registerTime; this.mail=mail;} toString () {var str= `Dear ${this.userName} Hello! Welcome to register as our member. Your account number is ${this.u_id} and your registration time is ${this.registerTime}. Please take good care of your account number and password. If you have any questions, please send an email to ${this.mail}! Thank you! I wish you a happy life! `; return str;}} module.exports=MailTemplateModel

The above code encapsulates the content of the message to be sent into an object, and then uses the template syntax to concatenate the string.

Think about it: we write the content of the email in an external separate txt file, and then realize it through the replace of the String object. How can this function be realized?

3 、 child_process

You can create child processes and execute shell scripts.

4 、 node-readbility

A plug-in 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 tomcat30 minutes, so you need to set your own timeout.

7. Basic-auth plug-in

It is used for the simplest authentication method and is generally used for api requests.

8. Bcryptjs plug-in (always reports errors during bcrypt installation)

Used for hash treatment by adding salt.

9. Collection of reptiles:

(1) crawling static pages and api data: request+cheerio/jsdom,request is a request library, which can request post and get information. After obtaining html data, use a third-party parsing library to parse cheerio. For js dynamic rendered pages, you can consider using jsdom, but unfortunately, this is synchronized and is not a browser after all.

(2) crawling of dynamically rendered pages

Puppeteer: use chromiun browser, asynchronous request, very efficient, and open a lot of api to operate the browser, very convenient.

Nightmare:api is very convenient to use, using the browser in electron, although it has not been used, but it does not feel as flexible as puppeteer.

Jsdom: synchronization has made me give up its use. Same as selenium.

10 、 moment.js

This is a lightweight format parsing library, if you write your own format parsing function, you need several dozen lines of function code, which is very convenient to use.

This is the end of the content of "what are the plug-ins for nodejs"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report