尝试使用koa mount
构建我的应用程序路由,并将实际的端点存储在其他文件中以保持整洁,但不断出现未发现的错误。
const Koa = require('koa')
const cors = require('@koa/cors')
const mount = require('koa-mount')
const bodyParser = require('body-parser');
const positions = require('./routes/position')
const API_PORT = 3001;
const main = async () => {
const app = new Koa()
// Parse incoming requests data
app.use(bodyParser())
app.use(cors({
credentials: true
}))
app.use(mount('/position', await positions()))
return app
}
if (require.main === module) {
main().then(
(app) => app.listen(API_PORT), console.log(`LISTENING ON PORT ${API_PORT}`)
)
}
module.exports = main
这是带有端点的文件:
// Import the WebFramework for routing
const Koa = require('koa')
const route = require('koa-route')
const bitmexAPI = require('../keys')
module.exports = async () => {
const app = new Koa()
app.use(route.get('/open', async (ctx) => {
//Get Positions
console.log("yes")
const position = await bitmexAPI.Position.get()
console.log(res)
//Response
//ctx.status = 200
ctx.body = {
data : res.json({ success: true, data: positio})
}
}))
return app
}
我正在使用邮递员进行此呼叫,当我尝试使用get进行调用时,它会给我一个not found
,而对我进行张贴时,则会给我一个内部服务器错误。
欢迎任何帮助