From 69f503124ac34dbc9e263db15f52d585c21606fb Mon Sep 17 00:00:00 2001 From: Tangdy Date: Mon, 3 Apr 2023 09:25:44 +0800 Subject: [PATCH] docs: add Node.js example code (#51) --- README.md | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/README.md b/README.md index e18cd20..444d3f3 100644 --- a/README.md +++ b/README.md @@ -477,6 +477,60 @@ namespace Demo +
+Node.js 示例 +
+ +```javascript +const axios = require('axios'); +const querystring = require('querystring'); + +const MESSAGE_PUSHER_SERVER = 'https://msgpusher.com' +const MESSAGE_PUSHER_USERNAME = 'test' +const MESSAGE_PUSHER_TOKEN = '666' + +async function send_message(title, description, content) { + try { + const postData = querystring.stringify({ + title: title, + desp: description, + content: content, + token: MESSAGE_PUSHER_TOKEN, + }) + + const response = await axios.post(`${MESSAGE_PUSHER_SERVER}/push/${MESSAGE_PUSHER_USERNAME}`, postData, { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + }) + if (response.data.success) { + return response.data + } + } catch (error) { + if (error.response) { + return error.response.data + } else { + throw error + } + + } +} + +send_message('标题', '描述', '**Markdown 内容**') + .then((response) => { + if (response.success) { + console.log('推送成功:', response) + } else { + console.log('推送失败:', response) + } + }, (error) => { + console.log(error.message); + }) + +``` +
+
+ 欢迎 PR 添加更多语言的示例。