Skip to content

Commit

Permalink
solve Es-lint issue
Browse files Browse the repository at this point in the history
Relates #33
  • Loading branch information
ranasobeid95 committed Aug 1, 2019
1 parent 93ec7f3 commit 24e2a4a
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 104 deletions.
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
<li id="container__ingredient3"></li>
</ul>
</section>
<script src="./request.js" type="application/javascript"></script>
<script src="./js/request.js" type="application/javascript"></script>
</body>
</html>
23 changes: 23 additions & 0 deletions public/js/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const url = '/search';
const drinkImg = document.getElementById('container__img');
const ingredient1 = document.getElementById('container__ingredient1');
const ingredient2 = document.getElementById('container__ingredient2');
const ingredient3 = document.getElementById('container__ingredient3');

document.getElementById('search_form').addEventListener('submit', (e) => {
e.preventDefault();
const input = document.getElementById('search__input').value;
fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ input }),
})
.then(res => res.json())
.then((res) => {
drinkImg.src = res.drinks[0].strDrinkThumb;
ingredient1.textContent = res.drinks[0].strIngredient1;
ingredient2.textContent = res.drinks[0].strIngredient2;
ingredient3.textContent = res.drinks[0].strIngredient3;
})
.catch(error => error.message);
});
23 changes: 0 additions & 23 deletions public/request.js

This file was deleted.

18 changes: 5 additions & 13 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser')
const bodyParser = require('body-parser');

const app = express();
const controllers = require('./controllers/index')
const controllers = require('./controllers/index');

app.disable('x-powered-by');
app.set('port',process.env.PORT || 5000)
app.use(express.static(path.join(__dirname, '..','public')))
app.set('port', process.env.PORT || 5000);
app.use(express.static(path.join(__dirname, '..', 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended : false }));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(controllers);

module.exports = app;








17 changes: 8 additions & 9 deletions src/controllers/error.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const path = require('path');

exports.clienterror = (req, res) => {
res
.status(404)
.send('clinet error')
}
res
.status(404)
.send('clinet error');
};

exports.servererror = (err, req, res, next) => {
res
.status(500)
.send('server error')
}

res
.status(500)
.send('server error');
};
7 changes: 4 additions & 3 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const express = require('express');

const router = express.Router();
const request = require('./request')
const request = require('./request');
const error = require('./error');

router.post('/search',request.post)
router.post('/search', request.post);
router.use(error.clienterror);
router.use(error.servererror);

module.exports = router;
module.exports = router;
40 changes: 20 additions & 20 deletions src/controllers/request.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
const request = require('request');

const gifReqest = (url) => {
const gifPromise = new Promise((resolve,reject)=>{
request(url, (err, response ,body )=>{
if(err){
reject('Error : response is no JSON');
}
resolve(JSON.parse(body));
});

});
return gifPromise;
};

const gifPromise = new Promise((resolve, reject) => {
request(url, (err, response, body) => {
if (err) {
reject('Error : response is no JSON');
}
resolve(JSON.parse(body));
});
});
return gifPromise;
};


exports.post = (req,res) =>{
const value =req.body.input.trim();
const gifUrl = `https://www.thecocktaildb.com/api/json/v1/1/search.php?s=${value}`;
gifReqest(gifUrl)
.then(apiRes => {
res.send(apiRes)
})
exports.post = (req, res) => {
const value = req.body.input.trim();
const gifUrl = `https://www.thecocktaildb.com/api/json/v1/1/search.php?s=${value}`;
gifReqest(gifUrl)
.then((apiRes) => {
res.send(apiRes);
})
.catch(error => error.message);
}
};
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const app = require('./app');

app.listen(app.get('port'),()=>{
console.log(`listen to http://localhost:${app.get('port')}`)
})
app.listen(app.get('port'), () => {
console.log(`listen to http://localhost:${app.get('port')}`);
});
63 changes: 31 additions & 32 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@

const test = require('tape');
const supertest = require('supertest')
const app =require('../src/app')
const supertest = require('supertest');
const app = require('../src/app');


test("testing",(t)=>{
t.equal(5,5,"return 5")
t.end();
})
test('testing', (t) => {
t.equal(5, 5, 'return 5');
t.end();
});

test('testing at status code equal 200',(t) => {
supertest(app)
test('testing at status code equal 200', (t) => {
supertest(app)
.get('/')
.expect(200)
.expect('Content-Type', /html/)
.end((err,res) => {
t.error(err);
t.end();
})
})
.end((err, res) => {
t.error(err);
t.end();
});
});

test('testing at status code equal 404', t => {
supertest(app)
.get('/test')
.expect(404)
.expect('Content-Type', /html/)
.end((err, res) => {
t.error(err);
t.equal(res.text, 'clinet error', 'Expected got clinet error' )
t.end();
})
})
test('testing at status code equal 404', (t) => {
supertest(app)
.get('/test')
.expect(404)
.expect('Content-Type', /html/)
.end((err, res) => {
t.error(err);
t.equal(res.text, 'clinet error', 'Expected got clinet error');
t.end();
});
});

test('testing at route /search', t => {
supertest(app)
test('testing at route /search', (t) => {
supertest(app)
.post('/search')
.expect(200)
.expect('Content-Type', 'application/json')
.end((err,res) => {
t.error(err);
t.end();
})

})
.end((err, res) => {
t.error(err);
t.end();
});
});

0 comments on commit 24e2a4a

Please sign in to comment.