Skip to content

Commit

Permalink
add server files
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyz committed Oct 17, 2011
1 parent a9eb9fc commit 0b5b1ba
Show file tree
Hide file tree
Showing 17 changed files with 244 additions and 6 deletions.
4 changes: 2 additions & 2 deletions item_prices/item_prices.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ define(function(require) {
function load_data() {

var html = "";
if(product!=null) {
if(typeof(product) !== "undefined" && product!==null) {
var pic = product["pic"];
var title = product["title"];
html += "<div><img src=\""+pic+"\" width=80 style=\"border: #DDDDDD solid 1px;\"><br><b>"+title+"</b></div>";
}
var max = 10;
if(visit_users!=null)
if(typeof(visit_users)!== "undefined" && visit_users!==null)
for(var i=0; i< visit_users.length; i++) {
var user = visit_users[i];
if(user!=null && user.username!="" && user.username!=="undefined"){
Expand Down
8 changes: 4 additions & 4 deletions visit_once/manifest.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
TLabs.addFeature({ //特性添加方法,不可修改 name: '数据魔方入口',// 特性名称,说明,可自定义
version: '1.0.4',//特性的版本号,可自定义
TLabs.addFeature({
version: '1.0.5',
mods: [
{
matches: "*", //"www.taobao.com|*.taobao.com|*.tmall.com", // 特性适应的域,正则表达式,允许是 `*`
path: 'visit_once.js' //特性的主逻辑js名称,可自定义
matches: "*",
path: 'visit_once.js'
}
]
});
Expand Down
Binary file modified visit_once/package.zip
Binary file not shown.
9 changes: 9 additions & 0 deletions visit_once/server/IDEAS
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- Collaborative editing
- Backed by node.js
- Drag and drop layout/image uploading. Don't worry about how things are formatted.
- Git integration. Tag commit messages with comment/item IDs.
- Email integration. Email when someone comments on a comment, email reply threads are imported into the app.
- Branch off new documents from existing ones, reference parent documents.
- Comment on individual words/blocks of text, display these inband.
- SQLite or file based nosql database
- Quick zipping for backup/retiring documents.
11 changes: 11 additions & 0 deletions visit_once/server/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/***
* 如果开启了mongoDB,将下面代码注释去掉,
* 并将dbUserName, dbPassword和dbName都
* 替换成分配得到的值。即可查看 mongoDB
* 测试程序。否则则开启hello world程序。
***/
/*
var mongo = require("mongoskin");
var db_url = exports.db_url = "dbUserName:[email protected]:20088/dbName";
exports.db = mongo.db(db_url);
*/
9 changes: 9 additions & 0 deletions visit_once/server/config/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// global config options
function apply(app, express) {
app.configure(function(){
app.use(express.static(__dirname + '/public'));
});
require('./../config/environment/development.js').apply(app, express);
require('./../config/environment/production.js').apply(app, express);
};
exports.apply = apply;
7 changes: 7 additions & 0 deletions visit_once/server/config/environment/development.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// development environment
function apply(app, express) {
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
};
exports.apply = apply;
3 changes: 3 additions & 0 deletions visit_once/server/config/environment/production.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function apply(app, express){
};
exports.apply = apply;
21 changes: 21 additions & 0 deletions visit_once/server/config/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function apply(app, express) {

// todo: build an mvc setup so that we can redirect routes to controllers

app.get('/', function(request, response) {
response.render('index.jade', { layout: false });
});

// simple catch all, right now just look for a template with the name
app.get('/:template', function(request, response) {
response.render(request.params.template + '.jade', { layout: false });
});

/*
app.get('/:controller/:action/:id', function(request, response) {
// find some way to dispatch to a controller
});
*/

};
exports.apply = apply;
20 changes: 20 additions & 0 deletions visit_once/server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var http = require('http');
var url = require("url");
var logs = new Array();
//中文支持可以吗
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var uri = url.parse(req.url).pathname;
var query = url.parse(req.url, true).query || {};
if(uri == "/") {
res.end("yes,it is me,thanks. good");
}
else if (uri=="/put"){
var user= query.u;
if(logs.indexOf(user)<0 && user!="") //if user not exists
logs.push(user);
res.write("var visit_users = "+JSON.stringify(logs)+";");
res.end();
}
}).listen(80, "127.0.0.1");
console.log('Server running at http://127.0.0.1/');
4 changes: 4 additions & 0 deletions visit_once/server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name" : "visitonce",
"main" : "server.js"
}
24 changes: 24 additions & 0 deletions visit_once/server/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
devlog
======

devlog is a living design and development log.

Based off of the following blog post:
http://www.lostgarden.com/2011/05/game-design-logs.html

requirements
------------

node.js
express
jade

installing
----------

- Install node.js on your server
- Install npm
- npm install express
- npm install jade

Note: later, will try to build some sort of package manager
11 changes: 11 additions & 0 deletions visit_once/server/script/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var express =require('express'),
app = express.createServer();

// bootstrap routing
require('./../config/routes').apply(app, express);

// bootstrap settings
require('./../config/environment').apply(app, express);

// start server - hardocded port for now
app.listen(80)
34 changes: 34 additions & 0 deletions visit_once/server/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var http = require('http');
var url = require("url");
var visitors = require("./visitors");
var logs = new Array();
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var uri = url.parse(req.url).pathname;
var query = url.parse(req.url, true).query || {};
if(uri == "/") {
res.end("yes,it is me, kongwu site");
}
else if (uri=="/put"){
var user= query.u;
var msg = query.msg;
var thisurl = query.url;
if(logs.indexOf(user)<0 && user!="") //if user not exists
logs.push(user);
var list_func = function(){
visitors.getList(thisurl,function(data){
res.writeHead(200, {'Content-Type': 'application/json;charset=gbk'});
res.write("var visit_users = "+JSON.stringify(data)+";");
res.end();
});
}
if(user!="" && thisurl!="" && msg!=null && msg.length<50)
visitors.findUser(user,thisurl,msg,list_func);
else
list_func();
}
}).listen(80, "127.0.0.1");
console.log('Server running at http://127.0.0.1/');
//visitors.list();
//just for test
//visitors.getList();
1 change: 1 addition & 0 deletions visit_once/server/views/foo.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p bar!
2 changes: 2 additions & 0 deletions visit_once/server/views/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h1 HELLO WORLD!
h2 just a test
82 changes: 82 additions & 0 deletions visit_once/server/visitors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var mongoose = require('mongoose'),
//db = mongoose.connect('mongodb://localhost/kongwu');
db = mongoose.connect('mongodb://7w2oigjkbkmah:[email protected]:20088/94lg0rk0awkmo');
var Schema = mongoose.Schema;
var events = require("events");
Visitors = new Schema({
id : {type:Number,index: true, default: 1},
username : {type:String,index: true},
url : {type:String,index: true},
visit_time : { type: Date, default: Date.now },
visit_count: { type:Number,default: 1},
msg: {type:String}
}).method('update', function(){
this.visit_count += 1;
this.visit_time = new Date();
return this.visit_count;
});
var Visitor = mongoose.model('Visitors', Visitors);
//var username = "kongwu";
exports.findUser = function(username,url,msg,callback) {Visitor.findOne({"username":username,"url":url},function(err,visit) {
if(visit){ //exists
console.log("duplicated:"+username);
visit.update();
}
else {
var visit = new Visitor();
visit.username = username;
visit.url = url;
console.log("new user:"+username);
}
if(msg!="")
visit.msg = msg;
visit.save(function(err) {
if(!err) console.log("save sucess:"+username);
else console.log(err);
callback();
});
});
};
exports.list = function(){Visitor.find({},function(err,records) {
if(!err) {
records.forEach(function(record) {
//console.log('username='+record.username+",visit_count="+record.visit_count+",last_time="+record.visit_time.toString());
//record.remove();

});
}
})};
//self.prototype = new events.EventEmitter;
exports.getList = function(url,cb){
var logs = new Array();
//test-url: http://kongwu.cnodejs.net/put?u=cnsnoopy&msg=&url=trade.taobao.com%2F%2Ftrade%2Fitemlist%2Flist_bought_items.htm
//Visitor.find({"url":url}).sort({visit_count:1},function(err,records) {
//Visitor.find({"url":url}).sort([['last', 'descending']]).all(function (err, records) {
Visitor.find({url:url, msg:{ $ne : null }}, ['username','visit_count','visit_time','msg'], // Columns to Return
{
skip:0, // Starting Row
limit:10, // Ending Row
sort:{
visit_time: -1 //Sort by Date Added DESC
}
},function (err, records) {
//Visitor.find({"url":url},function(err,records) {
if(!err) {
records.forEach(function(record) {
//console.log('username='+record.username+",visit_count="+record.visit_count+",last_time="+record.visit_time.toString());
if(record.msg!="" && record.msg!=null && typeof(record.msg)!="undefined")
logs.push({"username":record.username,"cnt":record.visit_count,"last":record.visit_time.toString(),"msg":record.msg});

});
//console.log(logs);
}
else
console.log("error in getList 2");
cb(logs);
//res.write("ver visit_users = "+JSON.stringify(logs)+";");
//res.end();
});
};
//exports.users = logs;
console.log("db");
//db.connection.close();

0 comments on commit 0b5b1ba

Please sign in to comment.