})
// Set permissive CORS header
app.use( async(ctx, next) => {
ctx.set( 'Access-Control-Allow-Origin', '*')
returnnext()
})
// ADD ENDPOINTS HERE
constport = process.env.PORT || 3000
app
.use(router.routes())
.use(router.allowedMethods())
.listen(port, err => {
if(err) throwerr
console.log( `App Listening on Port ${port}`)
})
这段代码导入服务依赖环境,为Koa.js Node API Server设置简单日志和错误处理机制。
6.1 将服务端点与查询链接起来
这一节为Server端添加服务端点,以便暴露给Elasticsearch查询服务。
在server/app.js中//ADD ENDPOINTS HERE 之后插入如下代码:
/**
* GET /search
* Search for a term in the library
*/
router. get( '/search', async(ctx, next) => {
const{ term, offset } = ctx.request.query
ctx.body = awaitsearch.queryTerm(term, offset)
}
)
用docker-compose up -d --build重启服务端。在浏览器中,调用此服务。例如::3000/search?term=java。
返回结果看起来应该如下:
{
"took": 242,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 93,
"max_score": 13.356944,
"hits": [{
"_index": "library",
"_type": "novel",
"_id": "eHYHJmEBpQg9B4622421",
"_score": 13.356944,
"_source": {
"author": "Charles Darwin",
"title": "On the Origin of Species",
"location": 1080,
"text": "Java, plants of, 375."
},
"highlight": {
"text": [ "<em>Java</em>, plants of, 375."]
}
}, {
"_index": "library",
"_type": "novel",
"_id": "2HUHJmEBpQg9B462xdNg",
"_score": 9.030668,
"_source": {
"author": "Unknown Author",
"title": "The King James Bible",
"location": 186,
"text": "10:4 And the sons of Javan; Elishah, and Tarshish, Kittim, and Dodanim."
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-88488-11.html
前几天的报告还在说锐澳的成功的
#易烊千玺##易烊千玺献声小王子#千千