Skip to content

Instantly share code, notes, and snippets.

@johnty
Last active August 22, 2016 13:35
Show Gist options
  • Select an option

  • Save johnty/b0ba38419b6ef8b159a536ae641a6a4a to your computer and use it in GitHub Desktop.

Select an option

Save johnty/b0ba38419b6ef8b159a536ae641a6a4a to your computer and use it in GitHub Desktop.
sandbox
// Import Express
var express = require("express");
var app = express();
// Import Cors: cross origin resource
var cors = require("cors");
app.use(cors());
// Import Mongoose
var mongoose = require("mongoose");
// Connect to database called "mean_template" running on local machine
mongoose.connect('mongodb://localhost/mean_template', function (error) {
if(error) {
console.log(error);
} else {
console.log("successfully connected to mean_template");
}
});
// this is from the MEAN tutorial video, grabbing something called "
var Product = mongoose.model('Product', {name: String});
//var product = new Product({name: "TestApp"});
//product.save(function(err) {
// if(err) {console.log(err);}
// else { console.log('saved product name')}
//});
// Schema defs
//Set up schemas for our mapper related data
// note its mongoose.Schema (and not mongoose.schema!)
var Schema = mongoose.Schema;
var SourceSchema = new Schema({
signal: String,
device: String,
id: String
});
var DestinationSchema = new Schema({
signal: String,
device: String,
id: String
});
var ConnectionSchema = new Schema({
clipMax: String,
clipMin: String,
expression: String,
range: [Number, Number, Number, Number],
mode: String,
mute: Number
});
var SignalSchema = new Schema({
direction: String,
device: String,
name: String
});
// Model definitions
// the first parameter is the name of the collection to look for
// inside our database (e.g. there should be a collections called "sources", "destinations", etc)
var MapperSourceModel = mongoose.model('sources', SourceSchema);
var MapperDestinationModel = mongoose.model('destinations', DestinationSchema);
var MapperConnectionModel = mongoose.model('connections', ConnectionSchema);
var MapperSignalModel = mongoose.model('signals', SignalSchema);
// previous names, basically same as above.
//var Source = mongoose.model('sources', SourceSchema);
//var Destination = mongoose.model('destinations', DestinationSchema);
//var Connection = mongoose.model('connections', ConnectionSchema);
//var Signal = mongoose.model('signals', SignalSchema);
//var Product = mongoose.model('Product', {_id: String});
// the default request at root, i.e. 127.0.0.1:3000/ in browser will return whatever
// that gets found by the Product database object (defined above)
app.get("/", function (req, res) {
Product.find(function (err, products_) {
res.send(products_);
})
});
// if we make a request at 127.0.0.1:3000/getsources, we return
// the query from the SourceModel object
app.get("/getsources", function (req, res) {
MapperSourceModel.find(function (err, returned_sources) {
res.send(returned_sources);
})
});
// Start app server
// the express app runs on port 3000.
// if we look at the app.js, that part is the Angular stuff and if you look at the
// app controller, it actually talks to our express app by making http requests
// i.e. under the App controller, we have:
//
// var url = "http://localhost:3000";
//
// the loadProducts line simply calls the express server at the root location, which is defined via
// app.get("/") above. this then simply does a find on the Product model, which returns the Product model
//
//
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment