-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_COPIA_IDEAS.js
200 lines (143 loc) · 5.17 KB
/
server_COPIA_IDEAS.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// server.js
// BASE SETUP
// ==============================================
var express = require('express');
var http = require('http');
var bodyParser = require('body-parser');
var app = express();
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var port = process.env.PORT || 8080;
//----------------------------------------------------------------------------------------
var sql = require('mssql');
var config = {
user: 'dsinfo',
password: 'dsinfo12345', // 'localhost\\instance' to connect to named instance
server: 'emazzu-sql-2012.cloudapp.net',
database: 'dataOxy', // You can enable streaming globally
stream: true,
options: {
encrypt: true // Use this if you're on Windows Azure
}
}
connection = new sql.Connection(config, function(err) {
if (err != null) {
console.log(err);
}
// Query
});
//connection.close();
//----------------------------------------------------------------------------------------
// ROUTES
// ==============================================
// sample route with a route the way we're used to seeing it
app.get('/sample', function(req, res) {
res.send('this is a sample!');
var str_Query = 'select top 10000 Id, Nombre, Comentario from pozos order by Id';
// or: var request = connection.request();
var request = new sql.Request(connection);
//request.stream = true; // You can set streaming differently for each request
request.query(str_Query);
//}); // or request.execute(procedure);
request.on('error', function(err) {
console.log(err);
});
request.on('row', function(row) {
// Always emitted as the last one
console.log(row);
});
request.on('done', function(returnValue) {
// Always emitted as the last one
console.log('done');
});
// , function(err, recordset) {
// if (err != null) {
// console.log(err);
// }
// //console.log(recordset);
// //res.send(recordset);
// recordset.render;
// });
});
// we'll create our routes here
// get an instance of router
var router = express.Router();
/****************** Request stack ******************
*
* Express handles routing by a defining a rule based stack that all requests passes through.
* Each layer in the stack is defined by a function. The request is passed down the chain and until it
* matches a route. If that function matching the route returns a response the chain is exited.
* Otherwise the request passes down to the next layer in the stack.
*/
// -- Middleware --
// Middleware is stuff that filters all requests. Middleware ends by passing the request
// to the next function in line by calling next();
// BTW: writing the function below like app.use("/*", function(req, res, next) { .. }) would
// have given the same result.
router.use(function(req, res, next) {
// log each request to the console
console.log(req.method, req.url);
// continue doing what we were doing and go to the route
next();
});
// home page route (http://localhost:8080)
router.get('/', function(req, res) {
res.send('im the home page!');
});
// about page route (http://localhost:8080/about)
router.get('/about', function(req, res) {
res.send('im the about page!');
});
// Respond with a HTML string
// html page route (http://localhost:8080/html)
app.get("/html", function(req, res) {
res.send("<!DOCTYPE html><html><title>String</title><body><h1>HTLM String</h1></body><html>");
});
app.getAuthorAsJson = function () {
return {username : "patricjansson", name : "Patric Jansson", twitter : "@patricjansson"};
}
// Respond with json
// html page route (http://localhost:8080/json)
app.get("/json", function(req, res) {
res.json(app.getAuthorAsJson());
});
// -- Pre-method url parsing
// Express has a nice feature that, much like middleware,
// can extract data from the url before the actual method handle function
// is invoked. For example it would be nice to read the correct user from
// a database before invoking the routing.
// NOTE: This example will match all ":name" in any request
router.param('name', function(req, res, next, name) {
// do validation on name here
// blah blah validation
// log something so we know its working
console.log('doing name validations on ' + name);
// once validation is done save the new item in the req
req.name = name;
// go to the next thing
next();
});
// route with parameters (http://localhost:8080/hello/:name)
router.get('/hello/:name', function(req, res) {
res.send('hello ' + req.name + '!');
});
// apply the routes to our application
app.use('/', router);
// login routes
app.route('/login')
// show the form (GET http://localhost:8080/login)
.get(function(req, res) {
res.send('this is the login form');
})
// process the form (POST http://localhost:8080/login)
.post(function(req, res) {
console.log('processing');
res.send('processing the login form!');
});
// START THE SERVER
// ==============================================
http.createServer(app).listen(port);
//app.listen(port);
console.log('Magic happens on port ' + port);