PSheon / Node-API-skeleton

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Node.js express.js MongoDB JWT REST API - 中文初始模板

作者 Software License Tag Travis Codacy Badge

開始

這是一個基本的 REST API 模板,使用 async/await 編寫的 JavaScript。非常適用於為你的前端(Android、iOS、Vue、Reave、react、angular 或任何可以使用 API 的產品)建立一個入門級的 Web API。

為 Daniel Avellaneda 加油

主要開發人員為 Daniel Avellaneda ,如果可以請貢獻你的一些力量讓社群能夠持續活躍

Buy Me A Coffee

或者在推特追蹤 https://twitter.com/davellanedam 作者

產品特色

  • Multiple environment ready (development, production)
  • Custom email/password user system with basic security and blocking for preventing brute force attacks.
  • Compressed responses.
  • Secured HTTP headers.
  • CORS ready.
  • Cache ready (Redis).
  • HTTP request logger in development mode.
  • i18n ready (for sending emails in multiple languages).
  • User roles.
  • Pagination ready.
  • User profile.
  • Users list for admin area.
  • Cities model and controller example.
  • Login access log with IP, browser and country location (for country it looks for the header cf-ipcountry that CloudFlare creates when protecting your website).
  • API autogenerated documentation by Postman.
  • API collection example for Postman.
  • Testing with mocha/chai for API endpoints.
  • NPM scripts for cleaning and seeding the MongoDB database.
  • NPM script for keeping good source code formatting using prettier and ESLint.
  • Use of ESLint for good coding practices.
  • Mailer example with Nodemailer and Mailgun.
  • Ability to refresh token
  • JWT Tokens, make requests with a token after login with Authorization header with value Bearer yourToken where yourToken is the signed and encrypted token given in the response from the login process.

需求

  • Node.js 10+ (10.15.3)
  • MongoDB 3.6+
  • Redis 5.0+

如何安裝

使用 Git (推薦)

  1. 從 Github 中克隆專案。把 "base-api" 改成你的 API 專案名稱。
git clone https://github.com/PSheon/Node-API-skeleton.git ./base-api

手動下載 ZIP

  1. 下載專案
  2. 解壓縮到你的專案位置

安裝相依套件

cd base-api
npm install
npm update

建立環境(開發或產品)

  1. 在這個專案的根目錄下,你會發現一個名為 .env.examplease 的文件。
  2. 復制並創建一個新文件,然後將其重命名為 .env
  3. 文件 .env 已經被忽略,所以你永遠不會提交你的憑證密碼。
  4. 根據你的環境(開發或產品)更改文件的值。
  5. .env 上傳到你的環境服務器(開發或產品)。
  6. 如果你使用 postman 集合來嘗試端點,請將環境中的變量 server的值改為你的服務器的 url,開發模式下使用http://localhost:3000

重要提示: 默認情況下,Token 會在 3 天內過期(在.environment.example 中設置為 4320 分鐘)。你可以在 API 端點 GET /token 刷新 Token 。如果一切正常,你會得到一個新的 Token 。

郵件服務

為了確保 API 發送的郵件的可傳遞性,"Mailgun "在用戶註冊時被用於郵件發送,所以如果你想使用這個功能,請到他們的網站 https://www.mailgun.com註冊。

如果你想嘗試不同的方法也可以使用https://nodemailer.com來做這個 API,他們有不同的傳輸方式,比如:smtp。

i18n

語言是由請求中的表頭 "Accept-Language" 自動檢測的。所以你可以在請求中手動發送 locale,或者你的瀏覽器會發送它的默認語言,如果沒有發送 "Accept-Language" ,那麼將使用 "en" 語言作為默認語言。

運行專案

清空資料庫 & 加入樣本資料

有 3 个可用的指令: fresh, clean and seed.

npm run command
  • fresh 清空資料庫 & 加入動態樣本資料.
  • clean 清空資料庫.
  • seed 加入動態樣本資料.

開發 API(運行 API 服務器)

你可以利用 npm run dev 指令的輸出來確認服務器是否正在運行。

npm run dev
****************************
*    Starting Server
*    Port: 3000
*    NODE_ENV: development
*    Database: MongoDB
*    DB Connection: OK
****************************

運行測試

/test 目錄下的 /test 中包含了一個如何做測試的例子。

npm run test

格式化程式碼 Formatting

使用 prettier 格式化你的程式碼:

npm run format

格式化 Markdown 文件

使用 remark 格式化你的 Markdown 文件:

npm run remark

檢查程式碼 Linting

使用 ESLint 檢查你的程式碼:

npm run lint

使用 API

當一切都設置好了,可以使用 Postman 或其他任何 API 測試程式來測試 API。登入時默認的用戶名/密碼是admin@admin.com/12345

API documentation

https://documenter.getpostman.com/view/487539/RWaHwoLV

Postman API 測試集

你可以將 API 測試集導入到 Postman 中。點擊導入按鈕,選擇位於根目錄下的 postman-example.json

Go to manage environments to create environments for development, production, etc. On each of the environments you create you will need to: 進入 "manage environments" 新增開發、生產等環境設定。在你創建的每一個環境中:

  1. Create a new key authToken and within the /login request this value is automatically updated after a successfull login through a script located in the tests tab. Each time you make a request to the API it will send Authorization header with the token value in the request, you can check this on the headers of users or cities endpoints in the Postman example.

建立一個新的 key "authToken", 在 "/login" 請求中,這個值會在成功登入後的 "測試" 腳本自動更新。每次你向 API 提出請求時,它將發送帶有請求中的 "token"值的 "授權" 表頭,你可以在其他的例子中檢查用戶或城市端點的表頭。

測試範例:

let data = pm.response.json()

pm.environment.set('authToken', data.token)
  1. 新增一個新的 key API_URL 內容是 API 的端點位置,例如在開發環境中可以使用 http://localhost:3000

This is a REST API, so it works using the following HTTP methods:

  • GET(Read): 獲取一個 Item 的列表,或單一 Item
  • POST(Create)。創建一個 Item
  • PATCH(Update)。更新一個 Item
  • DELETE:刪除一個 Item

License

This project is open-sourced software licensed under the MIT License. See the LICENSE file for more information.

About

License:MIT License


Languages

Language:JavaScript 99.4%Language:HTML 0.6%