-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
68 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require('env2')('.env') | ||
const request = require('request'); | ||
const handlers = require('../server/handlers'); | ||
const getMoives = require('../queries/getMovies.js'); | ||
|
||
const search = (req, res, value) => { | ||
getMoives(value,(err,result)=>{ | ||
if(err){ | ||
console.log(err); | ||
}else { | ||
res.writeHead(200,{ 'content-type': 'application/json '}); | ||
const output=JSON.stringify(result); | ||
res.end(output); | ||
} | ||
}) | ||
}; | ||
|
||
module.exports = { search }; |
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,11 @@ | ||
const dbConnection = require('../database/db_connection.js'); | ||
|
||
const getMoives = (title,cb) => { | ||
const query = 'SELECT * FROM movies where title = $1'; | ||
dbConnection.query(query ,[title], (err, res) => { | ||
if (err) return cb(err); | ||
cb(null, res.rows); | ||
}); | ||
}; | ||
|
||
module.exports =getMoives; |
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,52 +1,64 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const querystring = require('querystring'); | ||
const fs = require('fs') | ||
const path = require('path') | ||
const querystring=require('querystring'); | ||
const searchMoive=require('../handlerfunction/searchMoive.js') | ||
|
||
const errorHandler = (request, response) => { | ||
response.writeHead(404, { 'content-type': 'text/html' }); | ||
response.end('<h1> Page Not Found </h1>'); | ||
}; | ||
response.writeHead(404, { 'content-type': 'text/html' }) | ||
response.end('<h1> Page Not Found </h1>') | ||
} | ||
|
||
const homeHandler = (request, response) => { | ||
const htmlPath = path.join(__dirname, '..', '..', 'public', 'index.html'); | ||
const htmlPath = path.join(__dirname, '..', '..', 'public', 'index.html') | ||
fs.readFile(htmlPath, (error, htmlFile) => { | ||
if (error) { | ||
response.writeHead(500, { 'content-Type': 'text/html' }); | ||
response.end('<h1> Server error! sorry</h1>'); | ||
return; | ||
response.writeHead(500, { 'content-Type': 'text/html' }) | ||
response.end('<h1> Server error! sorry</h1>') | ||
return | ||
} | ||
response.writeHead(200, { 'Content-Type': 'text/html' }); | ||
response.write(htmlFile); | ||
response.end(); | ||
}); | ||
}; | ||
response.writeHead(200, { 'Content-Type': 'text/html' }) | ||
response.write(htmlFile) | ||
response.end() | ||
}) | ||
} | ||
|
||
const publicHandler = (request, response) => { | ||
const extension = request.url.split('.')[1]; | ||
const extension = request.url.split('.')[1] | ||
const contentTypeMapping = { | ||
html: 'text/html', | ||
css: 'text/css', | ||
js: 'application/js', | ||
}; | ||
js: 'application/js' | ||
} | ||
if (!contentTypeMapping[extension]) { | ||
response.writeHead(404, { 'Content-Type': 'text/html' }); | ||
response.end('<h1> Not Found </h1>'); | ||
response.writeHead(404, { 'Content-Type': 'text/html' }) | ||
response.end('<h1> Not Found </h1>') | ||
} else { | ||
const filePath = path.join(__dirname, '..', '..', 'public', request.url); | ||
const filePath = path.join(__dirname, '..', '..', 'public', request.url) | ||
fs.readFile(filePath, (error, file) => { | ||
if (error) { | ||
errorHandler(request, response); | ||
errorHandler(request, response) | ||
} | ||
response.writeHead(200, { 'content-type': contentTypeMapping[extension] }); | ||
response.end(file); | ||
}); | ||
response.writeHead(200, { 'content-type': contentTypeMapping[extension] }) | ||
response.end(file) | ||
}) | ||
} | ||
} | ||
|
||
const searchMoviesHandler = (request, response) => { | ||
if (!request.url.includes('/search?value=')) { | ||
errorHandler(request, response); | ||
}else { | ||
const parsedQuery = querystring.parse(request.url.split('?')[1]); | ||
searchMoive.search(request, response, parsedQuery.value); | ||
} | ||
}; | ||
|
||
|
||
|
||
module.exports = { | ||
homeHandler, | ||
publicHandler, | ||
errorHandler | ||
errorHandler, | ||
searchMoviesHandler, | ||
|
||
}; | ||
} |