-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
178 lines (134 loc) · 3.92 KB
/
app.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, tasks = require('./routes/tasks')
, http = require('http')
, path = require('path')
, util = require('util')
, fs = require('fs') // fileSystem /files
, mongoose = require('mongoose') // MongoDB /tasks
, mysql = require('mysql') // MySQL Database /blog
, async = require('async') // asynchronous utilities
, cmdr = require('commander') // command line utility
, request = require('request'); // request utilities
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(__dirname + '/other'));
}).configure('development', function(){
app.use(express.errorHandler());
});
var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var io = require('socket.io').listen(server)
, nicknames = [];
/**
* Tasks App GET Routes
*/
app.get('/', routes.index);
app.get('/users', user.list); // Authentication?...OAuth?
app.get('/SAMPLE', routes.SAMPLE); // demo
app.get('/map', routes.map); // leaflet
app.get('/chat', routes.chat); // socket.io
app.get('/blog', routes.blog); // mysql
app.get('/files', routes.files); // fs
// tasks uses MongoDB
app.get('/tasks', tasks.tasks);
app.get('/tasks/new', tasks.new);
app.get('/tasks/:id', tasks.task);
app.get('/tasks/:id/edit', tasks.edit);
app.get('/tasks/:id/delete', tasks['delete']);
/**
* Tasks App POST Routes
*/
app.post('/tasks', tasks.post);
/**
* Tasks App PUT Routes
*/
app.put('/tasks/:id', tasks.put);
/**
* Tasks App DELETE Routes
*/
app.del('/tasks/:id', tasks.del);
io.sockets.on('connection', function(soc){
soc.emit('connected', {text: 'connected'});
soc.on('nickname', function(data, callback){
if(nicknames.indexOf(data) != -1){
callback(false);
}else{
nicknames.push(data);
soc.nickname = data;
io.sockets.emit('nicknames', nicknames);
console.log('Nicknames are', nicknames);
callback(true);
}
});
soc.on('user message', function(data){
io.sockets.emit('user message', {
nick: soc.nickname,
message: data
});
});
soc.on('disconnect', function(){
nicknames.splice(nicknames.indexOf(soc.nickname), 1);
soc.broadcast.emit('nicknames', nicknames);
});
});
/**
* MongoDB
*/
mongoose.connect('mongodb://localhost/isomorph', function(err){
if(!err){
console.log('connected to mongoDB successfully');
}else{
throw err;
}
});
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
Task = new Schema({
task: String
}), Task = mongoose.model('Task', Task);
/**
* MySQL
*/
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost', // 14559 60591 15165 3306 3000 ???
user : 'root',
password : '334583oh!4'
});
connection.connect(function(){
console.log('successfully connected to mysql!');
});
/*
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
*/
connection.end();
cmdr
.version('0.0.1')
.option('-p, --peppers', 'Add peppers')
.option('-P, --pineapple', 'Add pineapple')
.option('-b, --bbq', 'Add bbq sauce')
.option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
.parse(process.argv);
console.log('you ordered a pizza with:');
if (cmdr.peppers) console.log(' - peppers');
if (cmdr.pineapple) console.log(' - pineappe');
if (cmdr.bbq) console.log(' - bbq');
console.log(' - %s cheese', cmdr.cheese);