使用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请求:
参考链接:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步