2020年最推薦的14個最有用NodeJS庫
Express
快速,簡單,極簡的節點Web框架
每週下載
1100萬
cheerio
Cheerio解析標記(例如HTML),並提供用於遍歷/操縱結果資料結構的APIconst cheerio = require(\’cheerio\’);
const $ = cheerio.load(\’
- …
\’);
每週下載
420萬
nodemailer
從Node.js傳送電子郵件
const nodemailer = require("nodemailer");
let testAccount = await nodemailer.createTestAccount();
let transporter = nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 587,
secure: false,
auth: {
user: testAccount.user,
pass: testAccount.pass
}
});
let info = await transporter.sendMail({
from: \'"Fred Foo " <[email protected]>\',
to: "[email protected], [email protected]",
subject: "Hello ✔",
text: "Hello world?",
html: "Hello world? "
});
每週下載
98萬
socket.io
Socket.IO支援基於事件的實時雙向通訊const server = require(\'http\').createServer();
const io = require(\'socket.io\')(server);
io.on(\'connection\', client => {
client.on(\'event\', data => { ... });
client.on(\'disconnect\', () => { ... });
});
server.listen(3000);
好處
· 實施實時分析,二進位制流,例項訊息傳遞和文件協作
· 知名使用者包括Microsoft Office,Yammer和Zendesk
每週下載
3M
Faker
在瀏覽器和node.js中生成大量假資料var faker = require(\'faker\');
var randomName = faker.name.findName(); // Rowan Nikolaus
var randomEmail = faker.internet.email(); // [email protected]
var randomCard = faker.helpers.createCard(); // random contact card
好處
· 在API後端構建尚未完成的情況下構建前端UI並與資料進行互動
· 多種API方法,包括地址,公司,資料庫,影象,名稱(firstName,lastName)
每週下載
140萬
morgan
Node.js的HTTP請求記錄器中介軟體例如GET / 200 51.267 ms — 1539
morgan
(\':method :url :status :res[content-length] - :response-time ms\')
---
var express = require(\'express\')
var morgan = require(\'morgan\')
var app = express()
app.use(morgan(\'combined\'))
app.get(\'/\', function (req, res) {
res.send(\'hello, world!\')
})
好處
· 將請求記錄在控制檯,檔案,資料庫中
· 除錯和日誌歷史記錄
每週下載
2M
http-errors
為Express,Koa,Connect等建立HTTP錯誤。app.use(function (req, res, next) {
if (!req.user)
return next(createError(401, \'Please login to view this page.\'))
next()
})
好處
· 易於傳送錯誤響應
· 許多錯誤屬性可用
expose
headers
message
status
statusCode
每週下載
27M
body-parser
Node.js主體解析中介軟體在處理程式之前在中介軟體中解析傳入的請求主體,該處理程式在req.body屬性下可用
var express = require(\'express\')
var bodyParser = require(\'body-parser\')
var app = express()
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
好處
· 解釋請求正文
· 許多選項,例如inflate,type,verify
每週下載
1300萬
sequelize
Sequelize是用於Postgres,MySQL,MariaDB,SQLite和Microsoft SQL Server的基於承諾的Node.js ORM它具有可靠的事務支援,關係,急切和延遲載入,讀取複製等功能
const sequelize = new Sequelize
(\'database\', \'username\', \'password\',
{
host: \'localhost\',
dialect: /* one of \'mysql\'
| \'mariadb\' | \'postgres\' | \'mssql\' */ });
好處
· Node.js ORM
每週下載
720K
passport
Passport是Node.js的Express相容身份驗證中介軟體Passport的唯一目的是對請求進行身份驗證,它通過一組稱為策略的可擴充套件外掛來完成
passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({ username: username }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!user.verifyPassword(password)) { return done(null,
false); }
return done(null, user);
});
}
));
好處
· Node.js身份驗證
· 與OAuth和OpenID整合(Facebook,Twitter等…登入)
每週下載
810K
Dotenv
Dotenv是一個零依賴模組,可將環境變數從.env檔案載入到process.env中將配置與程式碼分開儲存在環境中
require(\'dotenv\').config()
const db = require(\'db\')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
})
// .env file
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
好處
· 載入環境變數,例如AWS,sql使用者名稱,部署,連線到其他工具所需的密碼
· 將配置與程式碼分開儲存在環境中
每週下載
10M
multer
Multer是用於處理multipart / form-data的node.js中介軟體,主要用於上傳檔案var express = require(\'express\')
var multer = require(\'multer\')
var upload = multer({ dest: \'uploads/\' })
var app = express()
app.post(\'/profile\',
upload.single(\'avatar\'), function (req, res, next) {
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
})
app.post(\'/photos/upload\',
upload.array(\'photos\', 12), function (req, res, next) {
// req.files is array of `photos` files
// req.body will contain the text fields, if there were any
})
好處
· 易於上傳多部分/表單資料檔案
每週下載
92000
axios
基於Promise的HTTP客戶端,用於瀏覽器和node.jsconst axios = require(\'axios\');
// Make a request for a user with a given ID
axios.get(\'/user?ID=12345\')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.finally(function () {
// always executed
});
好處
· 從node.js發出HTTP請求
· 從瀏覽器發出XMLHttpRequests
· 支援Promise API
每週下載
960萬
CORS
CORS是用於提供Connect / Express中介軟體的node.js程式包,可用於啟用具有各種選項的CORSvar express = require(\'express\')
var cors = require(\'cors\')
var app = express()
app.use(cors())
app.get(\'/products/:id\', function (req, res, next) {
res.json({msg: \'This is CORS-enabled for all origins!\'})
})
app.listen(80, function () {
console.log(\'CORS-enabled web server listening on port 80\')
})
好處
· 輕鬆處理CORS問題
每週下載
370萬