当前位置: 首页 > 工具软件 > koa-sslify > 使用案例 >

关于使用koa实现线上https服务

墨雨华
2023-12-01
// 引入https 以及 koa-ssl
const https = require('https')
const sslify = require('koa-sslify').default
var fs= require("fs");
// 路径为证书放置的位置
 
const options = {
    key: fs.readFileSync('./httpskey/0_xcx.guanlovemay.xyz.key'),
    cert: fs.readFileSync('./httpskey/1_xcx.guanlovemay.xyz_bundle.pem'),
   }
const {connect,initSchemas} =require ('./database/init.js')
const Koa=require('koa');
const mongoose = require('mongoose');
const app=new Koa()

const bodyParser = require('koa-bodyparser')
const cors = require('koa2-cors')
const Router = require('koa-router')
//文件上传使用到
const koaBody = require('koa-body');
app.use(koaBody({
    multipart: true,
    formidable: {
        maxFileSize: 200*1024*1024    // 设置上传文件大小最大限制,默认2M
    }
}));
//https
app.use(sslify()) 
app.use(bodyParser())
app.use(cors())
let user = require('./appApi/user.js')
let wechatauth = require('./appApi/wechatauth.js')
//装载所有子路由
let router = new Router()
router.use('/user',user.routes())
//校验微信小程序用户信息
router.use('/wechatauth',wechatauth.routes())

//加载路由中间件
app.use(router.routes())
app.use(router.allowedMethods())
//立即执行
/* ;( async ()=>{
    await connect()
    initSchemas()
 })() */
 ;( async ()=>{
     https.createServer(options, app.callback()).listen(2888, (err) => {
        if (err) {
          console.log('服务启动出错', err);
        } else {
            ;( async ()=>{
                await connect()
                initSchemas()
                console.log('hunlian Server starting at port 2688')
            })()  
        }	
      }); 

 })() 

/*  app.listen(2888,()=>{
    console.log('hunlian Server starting at port 2888')
}) */

//http://localhost:2888/user
 类似资料: