위 챗 애플 릿 설정 - 서버 nodejs 버 전

2528 단어 js
1. 위 챗 애플 릿 서버 도 메 인 이름 설정
   1. 여기 서 제 가 말씀 드 리 고 싶 은 것 은 서버 와 상호작용 을 하 는 것 과 관련 이 있다 면 서버 도 메 인 이름 은 https 프로 토 콜 을 지원 해 야 합 니 다.아 리 운 과 텐 센트 운 에 서 는 https 인증 서 를 무료 로 신청 할 수 있다.저 는 텐 센트 클 라 우 드 를 사 용 했 습 니 다. 작업 절차 링크: 텐 센트 와 https 신청 입 니 다.
   2. 서버 쪽 https 요청 설정 코드 는 다음 과 같 습 니 다. 저 는 koa2web 엔 드 프레임 워 크 (추천) 를 사용 합 니 다.
   
const koa = require('koa');

const path = require('path');

const webstatic = require('koa-static');

const fs = require('fs');
const enforceHttps = require('koa-sslify');
const http = require('http');
const https = require('https');

const router = require(path.join(__dirname, 'router', './router.js')).router;

//      
const staticPath = path.join(__dirname, 'static');

const staticConf = webstatic(staticPath);

const app = new koa();

app.use(enforceHttps({
    trustProtoHeader: false
}));

app.use(staticConf);


메시지 푸 시 서버 설정
  1. token 을 작성 한 후 제출 을 클릭 하면 자동 으로 서버 url 주 소 를 방문 합 니 다. 따라서 요청 주 소 를 미리 설정 하여 get 요청 을 해 야 합 니 다.
  2. 검사 방식 은 sha 1 입 니 다. sha 1 모듈 을 설치 해 야 합 니 다. 서명 검사 에 실패 하면 매개 변수 이름 대신 값 을 사전 순서 로 정렬 하 는 지 확인 하 십시오. 구체 적 인 코드 는 다음 과 같 습 니 다.
    
//      nginx         
const options = {
    key: fs.readFileSync(path.join(__dirname, 'ssls', 'XX.key')),
    cert: fs.readFileSync(path.join(__dirname, 'ssls', 'XX.crt'))
};

router(app);

http.createServer(app.callback()).listen(80);
https.createServer(options, app.callback()).listen(443);
/**
 * @version 1.0        
 * 
 */
const path = require('path');

const util = require(path.join(__dirname, '../', 'util.js')).Util;

const wechatLanuch = (ctx) => {

    console.log('       ');

    let param = ctx.query;

    console.log('      ');

    console.log(param);

    if (param) {

        let sign = param.signature;

        let very = [param.timestamp, param.nonce];

        let verySign = util.encodeData(very);

        //     
        console.log(verySign);

        console.log(sign);

        if (sign == verySign) {

            ctx.response.body = param.echostr;

            console.log('    ');

        } else {

            ctx.response.body = { 'msg': 'vaild error' };
        }
    }

};

module.exports = { wechatLanuch };

이상 은 주의 점 입 니 다. 기본 설정 이 끝나 면 애플 릿 을 즐겁게 개발 할 수 있 습 니 다.

좋은 웹페이지 즐겨찾기