en.javascript.info/3-more/10-ajax/2-ajax-nodejs/example/index.js
Ilya Kantor 87bf53d076 update
2014-11-16 01:40:20 +03:00

31 lines
822 B
JavaScript
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var http = require('http');
var url = require('url');
var querystring = require('querystring');
var static = require('node-static');
var file = new static.Server('.');
function accept(req, res) {
// если URL запроса /vote, то...
if (req.url == '/vote') {
// через 1.5 секунды ответить сообщением
setTimeout(function() {
res.end('Ваш голос принят: ' + new Date());
}, 1500);
} else {
// иначе считаем это запросом к обычному файлу и выводим его
file.serve(req, res); // (если он есть)
}
}
// ------ этот код запускает веб-сервер -------
if (!module.parent) {
http.createServer(accept).listen(8080);
} else {
exports.accept = accept;
}