[英]rest-hapi standalone endpoint not returning handler results
請原諒我,如果這是一個愚蠢的問題,但我最近一次使用javascript編寫的是差不多20年前...我這幾周都在重新學習javascript,我不確定我是否已經完成了這一切。
我正在使用帶有rest-hapi的hapi並希望添加一些獨立的端點 ,基本上是翻譯此Autodesk教程表單的后端部分。
我正在使用基本的rest-hapi示例主腳本,並嘗試使用以下代碼添加路由:
//api/forge.js
module.exports = function(server, mongoose, logger) {
const Axios = require('axios')
const querystring = require('querystring')
const Boom = require('boom')
const FORGE_CLIENT_ID = process.env.FORGE_CLIENT_ID
const FORGE_CLIENT_SECRET = process.env.FORGE_CLIENT_SECRET
const AUTH_URL = 'https://developer.api.autodesk.com/authentication/v1/authenticate'
const oauthPublicHandler = async(request, h) => {
const Log = logger.bind('User Token')
try {
const response = await Axios({
method: 'POST',
url: AUTH_URL,
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
data: querystring.stringify({
client_id: FORGE_CLIENT_ID,
client_secret: FORGE_CLIENT_SECRET,
grant_type: 'client_credentials',
scope: 'viewables:read'
})
})
Log.note('Forge access token retrieved: ' + response.data.access_token)
return h.response(response.data).code(200)
} catch(err) {
if (!err.isBoom){
Log.error(err)
throw Boom.badImplementation(err)
} else {
throw err
}
}
}
server.route({
method: 'GET',
path: '/api/forge/oauth/public',
options: {
handler: oauthPublicHandler,
tags: [ 'api' ],
plugins: {
'hapi-swagger': {}
}
}
})
}
代碼有效,我可以在nodejs控制台中顯示access_token,但是swagger沒有得到響應:
起初我認為異步函數不能用作處理程序,但我的hapi版本是17.4.0,它支持異步處理程序。
我究竟做錯了什么?
事實證明這是一個簡單的修復:我只需要在我的主腳本中指定Hapi服務器主機名!
問題出在CORS上,因為Hapi使用的是我的機器名而不是localhost。 運用
let server = Hapi.Server({
port: 8080,
host: 'localhost'
})
解決了我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.