gpt4 book ai didi

node.js - 尝试运行 Node index.js 时未找到错误

转载 作者:搜寻专家 更新时间:2023-10-31 23:57:12 25 4
gpt4 key购买 nike

我已经安装了 Node.js,现在我想运行一些模拟 API。

index.js:

const app = require('koa')()
const cors = require('koa-cors')
const logger = require('koa-logger')
const router = require('koa-router')()

function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}

function getRandomDeliveryLocation() {
let locs = [
{ lat: 22.319181, lng: 114.170008, address: 'Mong Kok' },
{ lat: 22.336093, lng: 114.155288, address: 'Cheung Sha Wan' },
{ lat: 22.335538, lng: 114.176169, address: 'Kowloon Tong' }
]

return locs[ getRandomInt( 0, locs.length ) ]
}

function getRandomDeliveryDescription( index ) {
if ( index % 3 === 0 ) {
return 'Deliver documents to Andrio'
}

let desp = [
'Deliver documents to Andrio',
'Gift pets to Leviero',
'Gift pets to Alan'
]
return desp[ getRandomInt( 0, desp.length ) ]
}

function getRandomDeliveryItem( index ) {
return {
id: index,
description: getRandomDeliveryDescription( index ),
imageUrl: 'https://s3-ap-southeast-1.amazonaws.com/lalamove-mock-api/images/pet-'
+ getRandomInt( 0, 9 ) + '.jpeg',
location: getRandomDeliveryLocation()
}
}

function delay(sec) {
return new Promise(r => setTimeout(r, sec * 1000))
}

router.get('/pets', function* () {

let cap = 70
let offset = parseInt( this.query.offset, 10 )
let limit = parseInt( this.query.limit, 10 )
if ( isNaN( offset ) || isNaN( limit ) || offset < 0 || limit < 0 ) {
this.status = 400
return
}

yield delay(getRandomInt(0, 5))
if (!getRandomInt(0, 9)) {
this.status = 500
return
}

this.body = []
for ( let i = offset; i < offset + limit && i < cap; i++ ) {
this.body.push( getRandomDeliveryItem( i ) )
}

})

app
.use(logger())
.use(cors())
.use(router.routes())
.use(router.allowedMethods())

app.listen(8080)
console.log('Mock server started at port 8080')

它给了我:

Not Found Error when opening the localhost:8080/pets.

我是否遗漏了什么以及如何解决它?

最佳答案

您可以使用 async/await 代替生成器函数,它与您发布的代码一起工作。这是它的样子(我进行了额外的更改以解决其他问题):

router.get('/pets', async function (ctx) {
let cap = 70
let offset = parseInt( ctx.request.query.offset, 10 )
let limit = parseInt( ctx.request.query.limit, 10 )
if ( isNaN( offset ) || isNaN( limit ) || offset < 0 || limit < 0 ) {
ctx.response.status = 400
return
}

await delay(getRandomInt(0, 5))
if (!getRandomInt(0, 9)) {
ctx.response.status = 500
return
}

ctx.response.body = []
for ( let i = offset; i < offset + limit && i < cap; i++ ) {
ctx.response.body.push( getRandomDeliveryItem( i ) )
}
})

这里的变化是:

  1. function * 中删除了 * 以阻止它成为生成器函数。
  2. 更改为 async function 以便可以使用 await
  3. 使用 await 而不是 yield 来等待您的 delay promise 解决。
  4. this.query 切换到 ctx.request.query
  5. this.statusthis.body 切换到 ctx.response.statusctx.response.body.

关于node.js - 尝试运行 Node index.js 时未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52344925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com