js中的正则表达式 使用Docker和Elasticsearch搭建全文本搜索引(12)

电脑杂谈  发布时间:2018-02-22 10:22:50  来源:网络整理

},

"highlight": {

"text": [ "10:4 And the sons of <em>Javan</em>; Elishah, and Tarshish, Kittim, and Dodanim."]

}

}

...

]

}

}

6.2 输入验证

此时服务端还是很脆弱,下面对输入参数进行检查,对无效或者缺失的输入进行甄别,并返回错误。

我们使用Joi和Koa-Joi-Validate库进行这种类型的验证:

/**

* GET /search

* Search for a term in the library

* Query Params -

* term: string under 60 characters

* offset: positive integer

*/

router. get( '/search',

validate({

query: {

term: joi. string().max( 60).required(),

offset: joi.number().integer().min( 0). default( 0)

}

}),

async(ctx, next) => {

const{ term, offset } = ctx.request.query

ctx.body = awaitsearch.queryTerm(term, offset)

}

)

现在如果重启服务端,并做一个缺失参数查询(:3000/search),将会返回HTTP 400错误,例如:Invalid URL Query - child "term" fails because ["term" is required]。

可以用docker-compose logs -f api 查看日志。

7. 前端应用

/search服务端硬件可以了,本节写一段简单前端web应用测试API。

7.0 Vue.js

本节使用Vue.js来开发前端。创建一个新文件/public/app.js:

const vm = new Vue ({

el: '#vue-instance',

data() {

return{

baseUrl: 'http://localhost:3000', // API url

searchTerm: 'Hello World', // Default search term

searchDebounce: null, // Timeout for search bar debounce

searchResults: [], // Displayed search results

numHits: null, // Total search results found

searchOffset: 0, // Search result pagination offset

selectedParagraph: null, // Selected paragraph object

bookOffset: 0, // Offset for book paragraphs being displayed

paragraphs: [] // Paragraphs being displayed in book preview window

}

},

async created () {

this.searchResults = await this.search() // Search for default term


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-88488-12.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...