-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
95 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |