Last active
June 27, 2018 12:17
-
-
Save ankitadhandha/43b7116008f101c622e6db0a12d4242b to your computer and use it in GitHub Desktop.
stackoverflow AMP autosuggest Answer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
To implement autosuggest, AMP examples use endpoints which are developed in node.js. | |
1. Declare necessary node.js modules: | |
var express = require('express'); | |
var app = express(); | |
2. Declare JSON object data as: | |
const autosuggestData = ["Denver, Colorado","Hartford, Connecticut","Dover, Delaware","Tallahassee, Florida"]; | |
3. | |
app.use('/advanced/autosuggest/search_list', function(req, res) { | |
assertCors(req, res, ['GET']); | |
const MAX_RESULTS = 4; | |
const query = req.query.q; | |
if (!query) { | |
res.json({ | |
items: [{ | |
query: "", | |
results: | |
Data.slice(0, MAX_RESULTS) | |
}] | |
}); | |
} else { | |
const lowerCaseQuery = query.toLowerCase(); | |
const filtered = autosuggestData.filter(l => l.toLowerCase().includes(lowerCaseQuery)); | |
res.json({ | |
query: query, | |
items: [{results: filtered.slice(0, MAX_RESULTS)} | |
]}); | |
}}); | |
app.listen(app.get('port'), function() { | |
console.log('Express Started on http://localhost:' + app.get('port') + ''); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment