Permalink
Browse files

add ongoose db for store data

  • Loading branch information...
william.jiang
william.jiang committed Jul 1, 2012
1 parent faa1a1c commit 752cffc262243692f75807db6c4418e1998f7087
View
@@ -1 +1,3 @@
node_modules/
+test/
+test*
View
162 app.js
@@ -1,69 +1,93 @@
-/**
- * Module dependencies.
- * uploaded images are stored under 'my_dir'/images/ directory which can be accessed and shared.
- */
-var my_dir = __dirname.'/upload-image/public/';
-
-var express = require('express'),
- routes = require('./routes');
-
-var app = module.exports = express.createServer();
-
-// Configuration
-app.configure(function(){
- app.set('views', __dirname + '/views');
- app.set('view engine', 'jade');
- app.use(express.bodyParser());
- app.use(express.methodOverride());
- app.use(app.router);
- app.use(express.static(__dirname + '/public'));
-});
-
-app.configure('development', function(){
- app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
-});
-
-app.configure('production', function(){
- app.use(express.errorHandler());
-});
-
-// Routes
-app.get('/', function(req, res){
- //app.get('/', routes.index); first time load ~/public/index.html
- var html = require('fs').readFileSync(__dirname+'/public/index.html');
- res.end(html);
-});
-
-app.post('/api/photos', function(req, res) {
- console.log(JSON.stringify(req.files));
-
- var serverPath = '/images/' + req.files.userPhoto.name;
-
- require('fs').rename(
- req.files.userPhoto.path,
- my_dir + serverPath,
- function(error) {
- if(error) {
- res.send({
- error: 'Ah crap! Something bad happened'
- });
- return;
- }
- res.send({
- path: serverPath
- });
- }
- );
-});
-
-app.listen(3000, function(){
- console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
-});
-
-
-var nowjs = require("now");
-var everyone = nowjs.initialize(app);
-
-everyone.now.distributeMessage = function(message){
- everyone.now.receiveMessage(message);
-};
+/**
+ * Module dependencies.
+ * uploaded images are stored under 'my_dir'/images/ directory which can be accessed and shared.
+ */
+var my_dir = __dirname+'/public/';
+
+var express = require('express'),
+ routes = require('./routes'),
+ mongoose = require('mongoose');
+
+require('./upimage');
+
+var app = module.exports = express.createServer();
+
+var Image = mongoose.model('UpImage');
+var image = new Image();
+
+// Configuration
+app.configure(function(){
+ app.set('views', __dirname + '/views');
+ app.set('view engine', 'jade');
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ app.use(app.router);
+ app.use(express.static(__dirname + '/public'));
+});
+
+app.configure('development', function(){
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+});
+
+app.configure('production', function(){
+ app.use(express.errorHandler());
+});
+
+// Routes
+app.get('/', function(req, res){
+ //app.get('/', routes.index); first time load ~/public/index.html
+ var html = require('fs').readFileSync(__dirname+'/public/index.html');
+ res.end(html);
+});
+
+app.post('/api/photos', function(req, res) {
+ //console.log(JSON.stringify(req.files));
+ console.log(req.files);
+
+ var serverPath = '/images/' + req.files.userPhoto.name;
+
+ image.name = req.files.userPhoto.name;
+ image.size = req.files.userPhoto.size;
+ image.type = req.files.userPhoto.type;
+ image.modifed = req.files.userPhoto.lastModifiedDate;
+
+ image.save(function(err, res) {
+ if(err) { throw err; }
+ console.log(res);
+ });
+
+
+ require('fs').rename(
+ req.files.userPhoto.path,
+ my_dir + serverPath,
+ function(error) {
+ if(error) {
+ res.send({
+ error: 'Ah crap! Something bad happened'
+ });
+ return;
+ }
+ res.send({
+ path: serverPath
+ });
+ }
+ );
+
+ Image.find({}, function(err, docs) {
+ console.log(docs);
+ mongoose.disconnect();
+ });
+
+});
+
+app.listen(3000, function(){
+ console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
+});
+
+
+var nowjs = require("now");
+var everyone = nowjs.initialize(app);
+
+everyone.now.distributeMessage = function(message){
+ everyone.now.receiveMessage(message);
+};
View
@@ -1,9 +1,15 @@
-{
- "name": "application-name"
- , "version": "0.0.1"
- , "private": true
- , "dependencies": {
- "express": "2.5.8"
- , "jade": ">= 0.0.1"
- }
-}
+{
+ "name": "upload-images"
+ , "description": "Node Practical Test - William Jiang - upload Image from URL"
+ , "version": "1.0.0"
+ , "private": true
+ , "dependencies": {
+ "express": ">=2.5.8",
+ "mongodb": ">=1.0.2",
+ "mongoose": ">=2.6.5",
+ "node-proxy": ">=0.6.0",
+ "socket.io": ">=0.9.6",
+ "now": ">=0.8.1",
+ "request": ">=2.9.203"
+ }
+}
View
0 public/images/ajax-loader.gif 100755 → 100644
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN public/images/ajax-loader2.gif 100755 → 100644
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
0 public/images/email.png 100755 → 100644
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
0 public/images/youtube.png 100755 → 100644
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -1,81 +1,81 @@
-<!DOCTYPE html>
-<html>
-<head>
-<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-<title>Node Practical Test - William Jiang - upload Image from local machine</title>
-<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
-<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
-<script src="http://malsup.github.com/jquery.form.js"></script>
-<script src="/nowjs/now.js"></script>
-<script>
-$(document).ready(function() {
-
- // status('Choose a image file:)');
- now.receiveMessage = function(message){
- $(message).addClass('img_space').appendTo('div.display');
- //$(message).appendTo($('body')).wrap("<span/>");
- }
-
- $('#uploadForm').submit(function() {
- //status('Uploading the image file ...');
-
- $(this).ajaxSubmit({
-
- error: function(xhr) {
- status('Error: ' + xhr.status);
- },
-
- success: function(response) {
-
- if(response.error) {
- status('Opps, something bad happened');
- return;
- }
-
- var imageUrlOnServer = response.path;
-
- //status('Success, file uploaded to:' + imageUrlOnServer);
-
- // var img = $('<img/>').attr({'src':imageUrlOnServer, 'title':imageUrlOnServer.replace(/.*\//, '')});
- // $(img).appendTo($('body')).wrap("<span/>");
- var img = '<img src="'+imageUrlOnServer+'" title="'+imageUrlOnServer.replace(/.*\//, '')+'" />';
-
- // fresh all online pages.
- now.distributeMessage(img);
-
- }
- });
- // Have to stop the form from submitting and causing a page refresh - don't forget this
- return false;
- });
-
- function status(message) {
- $('#status').text(message);
- }
-
- $('#userPhotoInput').change(function(e) {
- e.preventDefault();
- $('#uploadForm').submit();
- return false;
- });
-});
-</script>
-</head>
-<body>
-<div class="box">
- <h2>Image Stream</h2>
- <hr />
- <p>In the field, input the full URL to your image. You can use the 'Post Image' button to upload it.</p>
- <div class="main">
- <form id="uploadForm"
- enctype="multipart/form-data"
- action="/api/photos"
- method="post">
- <input type="file" id="userPhotoInput" name="userPhoto" size="50" />
- </form>
- <div id="status" />
- <div class="display"></div>
- </div>
-</div>
-</body>
-</html>
+<!DOCTYPE html>
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<title>Node Practical Test - William Jiang - upload Image from local machine</title>
+<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
+<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
+<script src="http://malsup.github.com/jquery.form.js"></script>
+<script src="/nowjs/now.js"></script>
+<script>
+$(document).ready(function() {
+
+ // status('Choose a image file:)');
+ now.receiveMessage = function(message){
+ $(message).addClass('img_space').appendTo('div.display');
+ //$(message).appendTo($('body')).wrap("<span/>");
+ }
+
+ $('#uploadForm').submit(function() {
+ //status('Uploading the image file ...');
+
+ $(this).ajaxSubmit({
+
+ error: function(xhr) {
+ status('Error: ' + xhr.status);
+ },
+
+ success: function(response) {
+
+ if(response.error) {
+ status('Opps, something bad happened');
+ return;
+ }
+
+ var imageUrlOnServer = response.path;
+
+ //status('Success, file uploaded to:' + imageUrlOnServer);
+
+ // var img = $('<img/>').attr({'src':imageUrlOnServer, 'title':imageUrlOnServer.replace(/.*\//, '')});
+ // $(img).appendTo($('body')).wrap("<span/>");
+ var img = '<img src="'+imageUrlOnServer+'" title="'+imageUrlOnServer.replace(/.*\//, '')+'" />';
+
+ // fresh all online pages.
+ now.distributeMessage(img);
+
+ }
+ });
+ // Have to stop the form from submitting and causing a page refresh - don't forget this
+ return false;
+ });
+
+ function status(message) {
+ $('#status').text(message);
+ }
+
+ $('#userPhotoInput').change(function(e) {
+ e.preventDefault();
+ $('#uploadForm').submit();
+ return false;
+ });
+});
+</script>
+</head>
+<body>
+<div class="box">
+ <h2>Image Stream</h2>
+ <hr />
+ <p>In the field, input the full URL to your image. You can use the 'Post Image' button to upload it.</p>
+ <div class="main">
+ <form id="uploadForm"
+ enctype="multipart/form-data"
+ action="/api/photos"
+ method="post">
+ <input type="file" id="userPhotoInput" name="userPhoto" size="50" />
+ </form>
+ <div id="status" />
+ <div class="display"></div>
+ </div>
+</div>
+</body>
+</html>
View
@@ -0,0 +1,30 @@
+
+var mongohq_url = 'mongodb://williamjxj:Benjamin001@flame.mongohq.com:27075/williamjxj';
+
+var mongoose = require('mongoose')
+, Schema = mongoose.Schema;
+
+mongoose.connect(mongohq_url);
+
+// size: { type: String, match: /(\d\.\w)/ }
+var FileSchema = new Schema({
+ name: { type: String, index: true }
+ , type: { type: String }
+ , size: { type: String }
+ , modified: String
+ , date: { type: Date, default: Date.now }
+});
+
+FileSchema.path('name').set(function(v) {
+ return v;
+});
+
+FileSchema.pre('save', function(next) {
+ //notify(this.get('email'));
+ next();
+});
+
+var UpImage = mongoose.model('UpImage', FileSchema);
+
+exports.FileSchema = FileSchema;
+

0 comments on commit 752cffc

Please sign in to comment.