mirror of
https://github.com/tenrok/axios.git
synced 2026-06-14 18:42:33 +03:00
Improving examples
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
var fs = require('fs');
|
||||
var url = require('url');
|
||||
var path = require('path');
|
||||
var http = require('http');
|
||||
var argv = require('minimist')(process.argv.slice(2));
|
||||
var server;
|
||||
var dirs;
|
||||
|
||||
function listDirs(root) {
|
||||
var files = fs.readdirSync(root);
|
||||
var dirs = [];
|
||||
|
||||
for (var i=0, l=files.length; i<l; i++) {
|
||||
var file = files[i];
|
||||
if (file[0] !== '.') {
|
||||
var stat = fs.statSync(path.join(root, file));
|
||||
if (stat.isDirectory()) {
|
||||
dirs.push(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dirs;
|
||||
}
|
||||
|
||||
function sendResponse(res, statusCode, body) {
|
||||
res.writeHead(statusCode);
|
||||
res.write(body);
|
||||
res.end();
|
||||
}
|
||||
|
||||
function send200(res, body) {
|
||||
sendResponse(res, 200, body || '<h1>OK</h1>');
|
||||
}
|
||||
|
||||
function send404(res, body) {
|
||||
sendResponse(res, 404, body || '<h1>Not Found</h1>');
|
||||
}
|
||||
|
||||
function pipeFileToResponse(res, file, type) {
|
||||
if (type) {
|
||||
res.writeHead(200, {
|
||||
'Content-Type': type
|
||||
});
|
||||
}
|
||||
fs.createReadStream(path.join(__dirname, file)).pipe(res);
|
||||
}
|
||||
|
||||
|
||||
dirs = listDirs(__dirname);
|
||||
|
||||
server = http.createServer(function (req, res) {
|
||||
var url = req.url;
|
||||
|
||||
// Process axios itself
|
||||
if (/axios\.min\.js$/.test(url)) {
|
||||
pipeFileToResponse(res, '../dist/axios.min.js', 'text/javascript');
|
||||
return;
|
||||
}
|
||||
if (/axios\.min\.map$/.test(url)) {
|
||||
pipeFileToResponse(res, '../dist/axios.min.map', 'text/javascript');
|
||||
return;
|
||||
}
|
||||
if (/axios\.amd\.min\.js$/.test(url)) {
|
||||
pipeFileToResponse(res, '../dist/axios.amd.min.js', 'text/javascript');
|
||||
return;
|
||||
}
|
||||
if (/axios\.amd\.min\.map$/.test(url)) {
|
||||
pipeFileToResponse(res, '../dist/axios.amd.min.map', 'text/javascript');
|
||||
return;
|
||||
}
|
||||
|
||||
// Process /
|
||||
if (url === '/' || url === '/index.html') {
|
||||
var links = dirs.map(function (dir) {
|
||||
return '<li><a href="/' + dir + '">' + dir + '</a></li>';
|
||||
});
|
||||
send200(res, '<!doctype html><html><body><ul>' + links.join('') + '</ul>');
|
||||
return;
|
||||
}
|
||||
|
||||
// Format request */ -> */index.html
|
||||
if (/\/$/.test(url)) {
|
||||
url += 'index.html';
|
||||
}
|
||||
|
||||
// Format request /get -> /get/index.html
|
||||
var parts = url.split('/');
|
||||
if (dirs.indexOf(parts[parts.length - 1]) > -1) {
|
||||
url += '/index.html';
|
||||
}
|
||||
|
||||
// Process index.html request
|
||||
if (/index\.html$/.test(url)) {
|
||||
if (fs.existsSync(path.join(__dirname, url))) {
|
||||
pipeFileToResponse(res, url, 'text/html');
|
||||
} else {
|
||||
send404(res);
|
||||
}
|
||||
}
|
||||
|
||||
// Process server request
|
||||
else if (new RegExp('(' + dirs.join('|') + ')\/server').test(url)) {
|
||||
if (fs.existsSync(path.join(__dirname, url + '.js'))) {
|
||||
require(path.join(__dirname, url + '.js'))(req, res);
|
||||
} else {
|
||||
send404(res);
|
||||
}
|
||||
}
|
||||
else {
|
||||
send404(res);
|
||||
}
|
||||
});
|
||||
|
||||
server.listen(argv.p || 3000);
|
||||
Reference in New Issue
Block a user