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请求:
参考链接: