-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
171 lines (144 loc) · 5.7 KB
/
server.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
162
163
164
165
166
167
168
169
170
171
// ..........................................................
// Tasks MiddleWare Server
// ..........................................................
/*globals HOST PORT TasksNode*/
/**
This is a middleware (more like middle man) server for
the SproutCore Agile Tasks Manager application.
{{{
Prereq:
Download NodeJS: (http://s3.amazonaws.com/four.livejournal/20091117/node-v0.1.18.tar.gz)
or
Clone NodeJS: (git clone git://github.com/ry/node.git)
Compile and Install NodeJS (./configure && make && sudo make install)
Running:
After installing NodeJS you can run the server by issuing the following
command:
> nohup node server.js &
Integration:
Point the Proxy in Tasks' Buildfile to localhost:8989 and your off and running.
}}}
@author Joshua Holt
@version 0.1
@since 0.1
*/
// ..........................................................
// CONFIGURATION
// ..........................................................
TasksNode = {
/*
Used to set the host. When set to null serves on localhost
*/
HOST: null,
/*
Used to set the port for this server.
*/
PORT: 8989,
/*
Client Port (used to connect to a remote server's port)
*/
CLIENT_PORT: 8080,
/*
Client Headers (used to send custom headers to the remote server with a request)
*/
CLIENT_HEADERS: {"Accept": "application/json"},
/*
Client Host (The host your web client needs to be connected to)
*/
CLIENT_HOST: null,
/*
Used to indetify this server
*/
CUSTOM_USER_AGENT: "Tasks NodeJS Server",
/*
Common server (implements simple webserver functionality)
*/
CommonServer: require("./lib/common-server"),
/*
Common Client (implements simple web-client functionality)
*/
CommonClient: require('./lib/common-client'),
/*
System libraries (used to print to the console etc...)
*/
sys: require("sys"),
/*
Basic utils used everywhere
*/
utils: require('./lib/utils')
};
// ..........................................................
// BOOT SERVER && CLIENT
// ..........................................................
TasksNode.CommonServer.listen(TasksNode.PORT, TasksNode.HOST);
TasksNode.dbClient = TasksNode.CommonClient.bootClient(TasksNode.CLIENT_PORT, TasksNode.CLIENT_HOST);
// ..........................................................
// HANDLERS
// ..........................................................
TasksNode.Handlers = {
indexHandler: function(req, res){
res.simpleText(200, "Welcome to the Tasks NodeJS Server", TasksNode.CUSTOM_USER_AGENT);
},
authenticationHandler: function(req, res){
var method = req.method, json = "NONE", buffer = "";
res.simpleText(200, "AuthenticationHandler called with: %@ and %@".fmt(method, json), TasksNode.CUSTOM_USER_AGENT);
},
taskHandler: function(req, res){
var method = req.method, json = "NONE";
TasksNode.CommonClient.get(TasksNode.dbClient,
"/tasks%@".fmt(req.uri.path), TasksNode.CLIENT_HEADERS,
null, function(status, body){
if (status !== 200) {
res.simpleText(status, "ProjectHandler had a problem with your (%@) request.".fmt(method), TasksNode.CUSTOM_USER_AGENT);
}else{
res.simpleText(status, "ProjectHandler called with: %@ and %@".fmt(method, body), TasksNode.CUSTOM_USER_AGENT);
}
});
},
projectHandler: function(req, res){
var method = req.method, json = "NONE";
if (method === "POST") json = req.uri.params;
TasksNode.CommonClient.get(TasksNode.dbClient,
"/tasks%@".fmt(req.uri.path), TasksNode.CLIENT_HEADERS,
null, function(status, body){
if (status !== 200) {
res.simpleText(status, "ProjectHandler had a problem with your (%@) request.".fmt(method), TasksNode.CUSTOM_USER_AGENT);
}else{
res.simpleText(status, "ProjectHandler called with: %@ and %@".fmt(method, body), TasksNode.CUSTOM_USER_AGENT);
}
});
},
userHandler: function(req, res){
var method = req.method, json = "NONE", buffer = "";
if (method === "POST") {
req.addListener("body", function(chunk) {
buffer = buffer + chunk;
});
req.addListener("complete", function(chunk) {
json = JSON.parse(buffer).name;
res.simpleText(200, "UserHandler called with: %@ and %@".fmt(method, json), TasksNode.CUSTOM_USER_AGENT);
});
} else {
TasksNode.CommonClient.get(TasksNode.dbClient,
"/tasks%@".fmt(req.uri.path), TasksNode.CLIENT_HEADERS,
null, function(status, body){
if (status !== 200) {
res.simpleText(status, "UserHandler had a problem with your (%@) request.".fmt(method), TasksNode.CUSTOM_USER_AGENT);
}else{
res.simpleText(status, "UserHandler called with: %@ and %@".fmt(method, body), TasksNode.CUSTOM_USER_AGENT);
}
});
}
}
};
// ..........................................................
// ROUTES
// ..........................................................
TasksNode.CommonServer.get("/", TasksNode.Handlers.indexHandler);
TasksNode.CommonServer.get("/tasks-server/?", TasksNode.Handlers.indexHandler);
TasksNode.CommonServer.get("/tasks-server/user/?([0-9]+)?", TasksNode.Handlers.userHandler);
TasksNode.CommonServer.get("/tasks-server/task/?([0-9]+)?", TasksNode.Handlers.taskHandler);
TasksNode.CommonServer.get("/tasks-server/project/?([0-9]+)?", TasksNode.Handlers.projectHandler);
TasksNode.CommonServer.post("/tasks-server/user/?([0-9]+)?", TasksNode.Handlers.userHandler);
TasksNode.CommonServer.post("/tasks-server/task/?([0-9]+)?", TasksNode.Handlers.taskHandler);
TasksNode.CommonServer.post("/tasks-server/project/?([0-9]+)?", TasksNode.Handlers.projectHandler);