Skip to content

Instantly share code, notes, and snippets.

@Jonathancollinet
Created June 28, 2017 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jonathancollinet/c9511be0591135e253adab6e46c58d9b to your computer and use it in GitHub Desktop.
Save Jonathancollinet/c9511be0591135e253adab6e46c58d9b to your computer and use it in GitHub Desktop.
const express = require('express'),
app = express(),
mongoose = require('mongoose')
mongoose.connect("mongodb://localhost/test")
const userSchema = new mongoose.Schema({
username: { type: String, required: true, unique: true },
password: { type: String, required: true },
email: { type: String, required: true },
created_at: Date,
updated_at: Date
})
userSchema.index({username: 'text'}, {name: 'guyugugygu', weights: {username: 1}});
const User = mongoose.model('User', userSchema)
app.use(express.static(__dirname + '/'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/toto.html')
})
app.get('/search/:query', (req, res) => {
const query = req.params.query
User.find({ $text: { $search: query } }).exec((err, items) => {
if (err) {
res.status(404).json(err)
} else {
res.status(200).json(items)
}
})
})
app.listen(3000, () => {
console.log('http://localhost:3000')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment