DevTech101

DevTech101

npm install mongoose
app.js

var express = require('express');
var os = require('os');

// Mongoose import
var mongoose = require('mongoose');

// Mongoose connection to MongoDB (ted/ted is readonly)
mongoose.connect('mongodb://127.0.0.1/users', function (error) {
    if (error) {
        console.log(error);
    }
});

// Mongoose Schema definition
var Schema = mongoose.Schema;
var UserSchema = new Schema({
    hostname: String,
    loadavrg: String,
    totalmem: String,
    freemem: String
            /*    
             totalnetio: String,
             reacivenetio: String,
             transmitnetio: String,    
             totaldiskio: String,
             readdiskio: String,
             writediskio: String
             */
},
        {versionKey: false}
);

// Mongoose Model definition
var User = mongoose.model('users', UserSchema);

function updateMonitor() {
    var os_monitor = new User({
        hostname: os.hostname(),
        loadavrg: os.loadavg(),
        totalmem: os.totalmem(),
        freemem: os.freemem()
    });
    os_monitor.save(function (err, os_monitor) {
        if (err)
            return console.error(err);
        console.dir(os_monitor);
    });
}

var tid = setInterval(updateMonitor, 3000);

// Bootstrap express
var app = express();

// URLS management

app.get('/', function (req, res) {
    res.send("Show Users");
});


app.get('/users', function (req, res) {
    User.find({}, function (err, docs) {
        //console.log(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(3000);

References
http://blog.modulus.io/getting-started-with-mongoose
http://blog.ijasoneverett.com/2013/11/getting-started-with-mongoose-and-node-js-a-sample-comments-system/
https://www.airpair.com/javascript/complete-expressjs-nodejs-mongodb-crud-skeleton
OS Monitor
https://github.com/lfortin/node-os-monitor
Better OS utility
https://github.com/oscmejia/os-utils

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
%d bloggers like this: