This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
getPostPutDelete.js
102 lines (91 loc) · 4.66 KB
/
getPostPutDelete.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
/*
Express.js GET/POST example
Shows how handle GET, POST, PUT, DELETE
in Express.js 4.0
created 14 Feb 2016
by Tom Igoe
*/
var express = require('express'); // include express.js
var app = express(); // a local instance of it
var bodyParser = require('body-parser'); // include body-parser
var WebSocketServer = require('ws').Server // include Web Socket server
// you need a body parser:
app.use(bodyParser.urlencoded({extended: false})); // for application/x-www-form-urlencoded
// this runs after the server successfully starts:
function serverStart() {
var port = server.address().port;
console.log('Server listening on port '+ port);
}
app.get('/chunked', function(request, response) {
response.write('\n');
response.write(' `:;;;,` .:;;:. \n');
response.write(' .;;;;;;;;;;;` :;;;;;;;;;;: TM \n');
response.write(' `;;;;;;;;;;;;;;;` :;;;;;;;;;;;;;;; \n');
response.write(' :;;;;;;;;;;;;;;;;;; `;;;;;;;;;;;;;;;;;; \n');
response.write(' ;;;;;;;;;;;;;;;;;;;;; .;;;;;;;;;;;;;;;;;;;; \n');
response.write(' ;;;;;;;;:` `;;;;;;;;; ,;;;;;;;;.` .;;;;;;;; \n');
response.write(' .;;;;;;, :;;;;;;; .;;;;;;; ;;;;;;; \n');
response.write(' ;;;;;; ;;;;;;; ;;;;;;, ;;;;;;. \n');
response.write(' ,;;;;; ;;;;;;.;;;;;;` ;;;;;; \n');
response.write(' ;;;;;. ;;;;;;;;;;;` ``` ;;;;;`\n');
response.write(' ;;;;; ;;;;;;;;;, ;;; .;;;;;\n');
response.write('`;;;;: `;;;;;;;; ;;; ;;;;;\n');
response.write(',;;;;` `,,,,,,,, ;;;;;;; .,,;;;,,, ;;;;;\n');
response.write(':;;;;` .;;;;;;;; ;;;;;, :;;;;;;;; ;;;;;\n');
response.write(':;;;;` .;;;;;;;; `;;;;;; :;;;;;;;; ;;;;;\n');
response.write('.;;;;. ;;;;;;;. ;;; ;;;;;\n');
response.write(' ;;;;; ;;;;;;;;; ;;; ;;;;;\n');
response.write(' ;;;;; .;;;;;;;;;; ;;; ;;;;;,\n');
response.write(' ;;;;;; `;;;;;;;;;;;; ;;;;; \n');
response.write(' `;;;;;, .;;;;;; ;;;;;;; ;;;;;; \n');
response.write(' ;;;;;;: :;;;;;;. ;;;;;;; ;;;;;; \n');
response.write(' ;;;;;;;` .;;;;;;;, ;;;;;;;; ;;;;;;;: \n');
response.write(' ;;;;;;;;;:,:;;;;;;;;;: ;;;;;;;;;;:,;;;;;;;;;; \n');
response.write(' `;;;;;;;;;;;;;;;;;;;. ;;;;;;;;;;;;;;;;;;;; \n');
response.write(' ;;;;;;;;;;;;;;;;; :;;;;;;;;;;;;;;;;: \n');
response.write(' ,;;;;;;;;;;;;;, ;;;;;;;;;;;;;; \n');
response.write(' .;;;;;;;;;` ,;;;;;;;;: \n');
response.write(' \n');
response.write(' \n');
response.write(' \n');
response.write(' \n');
response.write(' ;;; ;;;;;` ;;;;: .;; ;; ,;;;;;, ;;. `;, ;;;; \n');
response.write(' ;;; ;;:;;; ;;;;;; .;; ;; ,;;;;;: ;;; `;, ;;;:;; \n');
response.write(' ,;:; ;; ;; ;; ;; .;; ;; ,;, ;;;,`;, ;; ;; \n');
response.write(' ;; ;: ;; ;; ;; ;; .;; ;; ,;, ;;;;`;, ;; ;;. \n');
response.write(' ;: ;; ;;;;;: ;; ;; .;; ;; ,;, ;;`;;;, ;; ;;` \n');
response.write(' ,;;;;; ;;`;; ;; ;; .;; ;; ,;, ;; ;;;, ;; ;; \n');
response.write(' ;; ,;, ;; .;; ;;;;;: ;;;;;: ,;;;;;: ;; ;;, ;;;;;; \n');
response.write(' ;; ;; ;; ;;` ;;;;. `;;;: ,;;;;;, ;; ;;, ;;;; \n');
response.write('\n');
response.end();
});
// this is the POST handler:
app.all('/*', function (request, response) {
console.log('Got a ' + request.method + ' request');
// the parameters of a GET request are passed in
// request.body. Pass that to formatResponse()
// for formatting:
console.log(request.headers);
if (request.method == 'GET') {
console.log(request.query);
} else {
console.log(request.body);
}
// send the response:
response.send('OK');
response.end();
});
// start the server:
var server = app.listen(8080, serverStart);
// create a WebSocket server and attach it to the server
var wss = new WebSocketServer({server: server});
wss.on('connection', function connection(ws) {
// new connection, add message listener
ws.on('message', function incoming(message) {
// received a message
console.log('received: %s', message);
// echo it back
ws.send(message);
});
});