Created
January 12, 2012 06:40
-
-
Save youtalk/1599088 to your computer and use it in GitHub Desktop.
Keyword search using Node.js server and jQuery-autocomplete client
This file contains hidden or 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
// 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