zoukankan      html  css  js  c++  java
  • 使用deno开发post请求,get请求,监测文件变化自动重启(类似于nodemon)

    launch.js:

    const run = () => {
      return Deno.run({
        cmd: ['deno', 'run', '--allow-net', '--allow-read', 'index.js'],
        cwd: 'app',
      })
    }
    let myPorcess = run()
    const watcher = Deno.watchFs('./app')
    for await (const event of watcher) {
      console.log(event)
      console.log('kill proceess')
      myPorcess.close()
      console.log('restart')
      myPorcess = run()
    }
    

    app/index.js:

    import { Application, Router, helpers } from 'https://deno.land/x/oak/mod.ts'
    const app = new Application()
    const router = new Router()
    
    router.get('/list', async (ctx) => {
      const { id } = helpers.getQuery(ctx, { mergeParams: true })
      ctx.response.body = {
        state: 1,
        data: { id },
        message: '成功',
      }
    })
    
    router.post('/login', async (ctx) => {
      const result = ctx.request.body()
      console.log(66678910)
      if (result.type === 'json') {
        const { username } = await result.value
        ctx.response.body = {
          state: 1,
          data: { username },
          message: '成功',
        }
      }
    })
    
    app.use(router.routes())
    app.use(router.allowedMethods())
    app.listen({ port: 8000 })
    console.log(8000)
    

     启动命令:

    deno run --allow-net --allow-read --allow-run launch.js

    自动重启:

    post请求:

     get请求:

    参考链接:

    https://zhuanlan.zhihu.com/p/143947500

    https://deno-tutorial.js.org/articles/index.html

  • 相关阅读:
    Abstract与Virtual
    List 常用方法
    控制数据采样分布 计算概念
    中期答辩感想
    软件开发之团队理解
    详细设计理解
    竞赛系统需求分析
    软件代码规范之理解
    AngularJS 整理资料
    AngularJS合集
  • 原文地址:https://www.cnblogs.com/xutongbao/p/15264303.html
Copyright © 2011-2022 走看看