updates
This commit is contained in:
parent
94c83e9e50
commit
cc5213b09e
79 changed files with 1341 additions and 357 deletions
52
5-network/08-xmlhttprequest/example.view/server.js
Normal file
52
5-network/08-xmlhttprequest/example.view/server.js
Normal file
|
@ -0,0 +1,52 @@
|
|||
let http = require('http');
|
||||
let url = require('url');
|
||||
let querystring = require('querystring');
|
||||
let static = require('node-static');
|
||||
let file = new static.Server('.');
|
||||
|
||||
function accept(req, res) {
|
||||
|
||||
if (req.url == '/load') {
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/plain',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Content-Length': 90000
|
||||
});
|
||||
|
||||
let i = 0;
|
||||
|
||||
let timer = setInterval(write, 1000);
|
||||
write();
|
||||
|
||||
function write() {
|
||||
res.write(String(i).repeat(10000));
|
||||
i++;
|
||||
if (i == 9) {
|
||||
clearInterval(timer);
|
||||
res.end();
|
||||
}
|
||||
|
||||
}
|
||||
} else if (req.url == '/json') {
|
||||
res.writeHead(200, {
|
||||
// 'Content-Type': 'application/json;charset=utf-8',
|
||||
'Cache-Control': 'no-cache'
|
||||
});
|
||||
|
||||
res.write(JSON.stringify({message: "Hello, world!"}));
|
||||
res.end();
|
||||
} else {
|
||||
file.serve(req, res);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ----- запуск accept как сервера из консоли или как модуля ------
|
||||
|
||||
if (!module.parent) {
|
||||
http.createServer(accept).listen(8080);
|
||||
} else {
|
||||
exports.accept = accept;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue