mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Adding support for node
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
var axios = require('../index');
|
||||
|
||||
var URL = 'http://127.0.0.1:3000/api';
|
||||
var BODY = {
|
||||
foo: 'bar',
|
||||
baz: 1234
|
||||
};
|
||||
|
||||
function handleSuccess(data) { console.log(data); }
|
||||
function handleFailure(data) { console.log('error', data); }
|
||||
|
||||
// GET
|
||||
axios.get(URL, { params: BODY })
|
||||
.then(handleSuccess)
|
||||
.catch(handleFailure);
|
||||
|
||||
// POST
|
||||
axios.post(URL, BODY)
|
||||
.then(handleSuccess)
|
||||
.catch(handleFailure);
|
||||
@@ -4,6 +4,16 @@ var path = require('path');
|
||||
var http = require('http');
|
||||
var server;
|
||||
|
||||
function pipeFileToResponse(res, file, type) {
|
||||
if (type) {
|
||||
res.writeHead(200, {
|
||||
'Content-Type': type
|
||||
});
|
||||
}
|
||||
|
||||
fs.createReadStream(path.join(__dirname, file)).pipe(res);
|
||||
}
|
||||
|
||||
server = http.createServer(function (req, res) {
|
||||
req.setEncoding('utf8');
|
||||
|
||||
@@ -17,12 +27,11 @@ server = http.createServer(function (req, res) {
|
||||
}
|
||||
|
||||
if (pathname === '/index.html') {
|
||||
fs.createReadStream(path.join(__dirname, pathname)).pipe(res);
|
||||
pipeFileToResponse(res, './client.html');
|
||||
} else if (pathname === '/axios.js') {
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/javascript'
|
||||
});
|
||||
fs.createReadStream(path.join(__dirname, '../dist/axios.js')).pipe(res);
|
||||
pipeFileToResponse(res, '../dist/axios.js', 'text/javascript');
|
||||
} else if (pathname === '/axios.map') {
|
||||
pipeFileToResponse(res, '../dist/axios.map', 'text/javascript');
|
||||
} else if (pathname === '/api') {
|
||||
var status;
|
||||
var result;
|
||||
Reference in New Issue
Block a user