secret
Created

  • Download Gist
so-storify.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
//Storify -- Khuram Malik
 
// Required Modules
var app = require('express').createServer()
, io = require('socket.io').listen(app)
, express = require('express')
, mongoose = require ('mongoose')
, Schema = mongoose.Schema;
 
app.listen(80);
 
app.get('/', function (req, res) {
res.sendfile((__dirname + '/story.html'));
});
 
 
app.use(express.static(__dirname + '/public'));
 
//Create Schema
 
var Lines = new Schema({
author : String,
text : String
});
 
var Story = new Schema ({
maxlines: {type: Number, default: 3}, // Max number of lines per user
date: {type: Date, default: Date.now},
title: String,
lines: [Lines]
});
 
mongoose.connect('mongodb://localhost/test');
 
//setup model and pass it schema
mongoose.model('Story', Story);
 
var StoryModel = mongoose.model('Story');
 
var story = new StoryModel();
 
// Fixed params
story.title = 'Last Samurai';
 
//Capture data from socket into schema
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('slog', function(data){
story.lines.push ({
author: 'Khuram',
text: data.my
});
story.save(function(err){
if (err) {throw err; }
console.log('saved story line');
});
});
});
 
 
//disconnect db
//mongoose.disconnect();

Don't disconnect mongo. That will happen before any connection is established. Connections will die automatically when you exit the node process.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.