Skip to content

Commit

Permalink
Replace depecated requests library with axios
Browse files Browse the repository at this point in the history
  • Loading branch information
luisddm committed Oct 10, 2020
1 parent 0684699 commit 6d53130
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
"start": "node ./bin/www"
},
"dependencies": {
"axios": "^0.20.0",
"body-parser": "^1.19.0",
"cheerio": "^1.0.0-rc.3",
"cookie-parser": "^1.4.5",
"debug": "^4.2.0",
"express": "^4.17.1",
"morgan": "^1.10.0",
"node-fetch": "^2.6.1",
"pug": "^3.0.0",
"request": "^2.88.2",
"serve-favicon": "^2.4.3"
Expand Down
53 changes: 26 additions & 27 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express');
const cheerio = require('cheerio');
const request = require('request');
const axios = require('axios');

const router = express.Router();

Expand Down Expand Up @@ -41,40 +41,39 @@ router.get('/', (req, res, next) => {
res.render('main', { title: 'Auvasa Scraper' });
});

router.get('/web/stop/:id', (req, res, next) => {
router.get('/web/stop/:id', async (req, res, next) => {
const uri = `${host}/${namespace}?codigo=${req.params.id}`;

// The structure of our request call
// The first parameter is our URL
// The callback function takes 3 parameters, an error, response status code and the html

request({ uri, encoding: 'binary' }, (error, response, html) => {
// First we'll check to make sure no errors occurred when making the request

if (!error) {
axios({
method: 'GET',
url: uri,
responseType: 'arraybuffer',
responseEncoding: 'binary',
})
.then((response) => {
const html = response.data.toString('binary');
res.render('index', getTimetable(html, +req.params.id));
} else {
res.render('error', { message: 'Error', error });
}
});
}).catch((error) => {
res.render('error', { message: 'Error', error: error.response });
});
});

router.get('/api/stop/:id', (req, res, next) => {
router.get('/api/stop/:id', async (req, res, next) => {
const uri = `${host}/${namespace}?codigo=${req.params.id}`;

// The structure of our request call
// The first parameter is our URL
// The callback function takes 3 parameters, an error, response status code and the html

request({ uri, encoding: 'binary' }, (error, response, html) => {
// First we'll check to make sure no errors occurred when making the request

if (!error) {
axios({
method: 'GET',
url: uri,
responseType: 'arraybuffer',
responseEncoding: 'binary',
})
.then((response) => {
const html = response.data.toString('binary');
res.json(getTimetable(html, +req.params.id));
} else {
res.json({ error });
}
});
})
.catch((error) => {
res.json({ error: error.response });
});
});

module.exports = router;

0 comments on commit 6d53130

Please sign in to comment.