Created
June 28, 2017 10:03
-
-
Save Jonathancollinet/c9511be0591135e253adab6e46c58d9b to your computer and use it in GitHub Desktop.
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
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