アプレットはサーバーとして Nodejs を使用し、Nodejs は MYSQL データベースに接続されます。
1.環境の構築
- 新しい空のフォルダーを作成します: Win + R で cmd コマンド インターフェイスに入り、実行します。
npm install express body-parser request
2.Nodejs の構成
- ディレクトリに新しいindex.jsファイルを作成し、次のコードを構成します。
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const app = express()
const PORT = 5008
app.use(bodyParser.json())
app.get('/',(req,res)=>{
res.send('Server is running!')
})
app.listen(PORT,()=>{
console.log(`Server is running on localhost:${
PORT}`);
})
- Nodejs を開始します: ターミナルに入力します。
node index.js
3. ミニプログラムとの対話
- サーバー側のコード実装: まだindex.jsファイル内にあります
const APP_ID = "wx4f9ef75353fd5bc3";
const APP_SECRET = "d9317db76db37df632d729ca0bdf1f2a";
app.get("access_token", (req, res) => {
const url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${
APP_ID}&secret=${
APP_SECRET}`;
request.get(url, (error, response, body) => {
if (!error && response.statusCode === 200) {