Skip to content

Instantly share code, notes, and snippets.

@youtalk
Created January 12, 2012 06:40
Show Gist options
  • Save youtalk/1599088 to your computer and use it in GitHub Desktop.
Save youtalk/1599088 to your computer and use it in GitHub Desktop.
Keyword search using Node.js server and jQuery-autocomplete client
// Server-side
app.get('/keyword/search',
function (req, res, next) {
var query = new RegExp('^' + req.query.q + '.*$', 'i');
var result = [];
mongodb.keywords.find(
{ word: query }, [ 'word' ], { limit: 5 },
function (err, keywords) {
keywords.forEach(function (k) {
result.push(k.word);
});
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(result.join('\n'));
});
});
// Database
var KeywordSchema = new Schema({
word: { type: String, required: true, index: { unique: true } }
});
module.exports.keywords = db.model('keywords', KeywordSchema);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment