Speed up development with full-stack environments for every branch.

Learn More

MangoDb Socket.io chat example [express, Node.js, mongoose and mongodb]

Forked from Querying MongoDB from Express using Mongoose.

3304 Runs 10321 Views 1244 Copies
Saved

Saved

anilari 1

anilari
published 3 years ago

    var express = require('express');
    
    // Mongoose import
    var mongoose = require('mongoose');
    
    // Mongoose connection to MongoDB (ted/ted is readonly)
    mongoose.connect('mongodb://ted:[email protected]:61797/theenlighteneddeveloper', function (error) {
        if (error) {
            console.log(error);
        }
    });
    
    // Mongoose Schema definition
    var Schema = mongoose.Schema;
    var UserSchema = new Schema({
        first_name: String,
        last_name: String,
        email: String
    });
    
    // Mongoose Model definition
    var User = mongoose.model('users', UserSchema);
    
    // Bootstrap express
    var app = express();
    
    // URLS management
    
    app.get('/', function (req, res) {
        res.send("<a href='/users'>Show Users</a>");
    });
    
    app.get('/users', function (req, res) {
        User.find({}, function (err, docs) {
            res.json(docs);
        });
    });
    
    app.get('/users/:email', function (req, res) {
        if (req.params.email) {
            User.find({ email: req.params.email }, function (err, docs) {
                res.json(docs);
            });
        }
    });
    
    // Start the server
    app.listen(80);
    
    Please login/signup to get access to the terminal.

    Your session has timed out.

    Dismiss (the page may not function properly).