mirror of
https://github.com/tenrok/axios.git
synced 2026-06-08 17:22:34 +03:00
Improving examples
This commit is contained in:
+2
-2
@@ -5,5 +5,5 @@ To run the examples:
|
||||
1. Clone this repo
|
||||
2. Run `npm install`
|
||||
3. Run `grunt build`
|
||||
4. Run `http-server -p 3000`
|
||||
5. Open http://localhost:3000/examples
|
||||
4. Run `npm run examples`
|
||||
5. Open http://localhost:3000
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
<ul id="orgs" class="list-unstyled"></ul>
|
||||
</div>
|
||||
|
||||
<script src="/dist/axios.min.js"></script>
|
||||
<script src="/axios.min.js"></script>
|
||||
<script>
|
||||
axios.all([
|
||||
axios.get('https://api.github.com/users/mzabriskie'),
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>AMD</title>
|
||||
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"/>
|
||||
</head>
|
||||
<body class="container">
|
||||
<h1>AMD</h1>
|
||||
|
||||
<div>
|
||||
<h3>User</h3>
|
||||
<div class="row">
|
||||
<img id="useravatar" src="" class="col-md-1"/>
|
||||
<div class="col-md-3">
|
||||
<strong id="username"></strong>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.16/require.min.js"></script>
|
||||
<script>
|
||||
requirejs.config({
|
||||
paths: {
|
||||
axios: '/axios.amd.min'
|
||||
}
|
||||
});
|
||||
|
||||
requirejs(['axios'], function (axios) {
|
||||
axios.get('https://api.github.com/users/mzabriskie')
|
||||
.then(function (user) {
|
||||
document.getElementById('useravatar').src = user.data.avatar_url;
|
||||
document.getElementById('username').innerHTML = user.data.name;
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -8,9 +8,9 @@
|
||||
<h1>axios.get</h1>
|
||||
<ul id="people" class="list-unstyled"></ul>
|
||||
|
||||
<script src="/dist/axios.min.js"></script>
|
||||
<script src="/axios.min.js"></script>
|
||||
<script>
|
||||
axios.get('people.json')
|
||||
axios.get('/get/server')
|
||||
.then(function (response) {
|
||||
document.getElementById('people').innerHTML = response.data.map(function (person) {
|
||||
return (
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
[
|
||||
var people = [
|
||||
{
|
||||
"name": "Matt Zabriskie",
|
||||
"github": "mzabriskie",
|
||||
@@ -23,4 +23,12 @@
|
||||
"twitter": "chrisesplin",
|
||||
"avatar": "878947"
|
||||
}
|
||||
]
|
||||
];
|
||||
|
||||
module.exports = function (req, res) {
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/json'
|
||||
});
|
||||
res.write(JSON.stringify(people));
|
||||
res.end();
|
||||
};
|
||||
@@ -9,11 +9,7 @@
|
||||
|
||||
<form role="form" class="form" onsubmit="return false;">
|
||||
<div class="form-group">
|
||||
<label for="url">URL</label>
|
||||
<input id="url" type="text" class="form-control"/>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="data">Body</label>
|
||||
<label for="data">JSON</label>
|
||||
<textarea id="data" class="form-control" rows="5"></textarea>
|
||||
</div>
|
||||
<button id="post" type="button" class="btn btn-primary">POST</button>
|
||||
@@ -21,15 +17,14 @@
|
||||
|
||||
<div id="output" class="container"></div>
|
||||
|
||||
<script src="/dist/axios.min.js"></script>
|
||||
<script src="/axios.min.js"></script>
|
||||
<script>
|
||||
(function () {
|
||||
var output = document.getElementById('output');
|
||||
document.getElementById('post').onclick = function () {
|
||||
var url = document.getElementById('url').value;
|
||||
var data = document.getElementById('data').value;
|
||||
|
||||
axios.post(url, JSON.parse(data))
|
||||
axios.post('/post/server', JSON.parse(data))
|
||||
.then(function (res) {
|
||||
output.className = 'container';
|
||||
output.innerHTML = res.data;
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
module.exports = function (req, res) {
|
||||
var data = '';
|
||||
|
||||
req.on('data', function (chunk) {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
req.on('end', function () {
|
||||
console.log('POST data received');
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/json'
|
||||
});
|
||||
res.write(JSON.stringify(data));
|
||||
res.end();
|
||||
});
|
||||
};
|
||||
@@ -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);
|
||||
@@ -16,7 +16,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="/dist/axios.min.js"></script>
|
||||
<script src="/axios.min.js"></script>
|
||||
<script>
|
||||
var ISO_8601 = /(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})Z/;
|
||||
function formatDate(d) {
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>axios - file upload example</title>
|
||||
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"/>
|
||||
</head>
|
||||
<body class="container">
|
||||
<h1>file upload</h1>
|
||||
|
||||
<form role="form" class="form" onsubmit="return false;">
|
||||
<div class="form-group">
|
||||
<label for="file">File</label>
|
||||
<input id="file" type="file" class="form-control"/>
|
||||
</div>
|
||||
<button id="upload" type="button" class="btn btn-primary">Upload</button>
|
||||
</form>
|
||||
|
||||
<div id="output" class="container"></div>
|
||||
|
||||
<script src="/axios.min.js"></script>
|
||||
<script>
|
||||
(function () {
|
||||
var output = document.getElementById('output');
|
||||
document.getElementById('upload').onclick = function () {
|
||||
var data = new FormData();
|
||||
data.append('foo', 'bar');
|
||||
data.append('file', document.getElementById('file').files[0]);
|
||||
|
||||
axios.put('/upload/server', data)
|
||||
.then(function (res) {
|
||||
output.className = 'container';
|
||||
output.innerHTML = res.data;
|
||||
})
|
||||
.catch(function (res) {
|
||||
output.className = 'container text-danger';
|
||||
output.innerHTML = res.data;
|
||||
});
|
||||
};
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
module.exports = function (req, res) {
|
||||
var data = '';
|
||||
|
||||
req.on('data', function (chunk) {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
req.on('end', function () {
|
||||
console.log('File uploaded');
|
||||
res.writeHead(200);
|
||||
res.end();
|
||||
});
|
||||
};
|
||||
@@ -6,6 +6,7 @@
|
||||
"scripts": {
|
||||
"test": "grunt test",
|
||||
"start": "node ./sandbox/server.js",
|
||||
"examples": "node ./examples/server.js",
|
||||
"coveralls": "cat coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
|
||||
},
|
||||
"repository": {
|
||||
@@ -47,6 +48,7 @@
|
||||
"karma-phantomjs-launcher": "^0.1.4",
|
||||
"karma-webpack": "^1.5.0",
|
||||
"load-grunt-tasks": "^3.1.0",
|
||||
"minimist": "^1.1.1",
|
||||
"webpack": "^1.7.2",
|
||||
"webpack-dev-server": "^1.7.0"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user