Last active
July 23, 2016 10:42
-
-
Save m-alikhizar/caa83eb33b50bbbce9b4454fda2dd794 to your computer and use it in GitHub Desktop.
NodeJS Express Routing
This file contains hidden or 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
'use strict'; | |
var router = require('./route'); | |
// var error_hander = require('./error_handler'); | |
var express = require('express'); | |
var app = express(); | |
const PORT = 1337; | |
app.use('/I/want/title/', router); | |
app.use('*', redirect_unmatched); | |
function redirect_unmatched(request, response) { | |
response.status(404) // HTTP status 404: NotFound | |
.send('Not found'); | |
} | |
console.log("listening start"); | |
app.listen(PORT); |
This file contains hidden or 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
{ | |
"name": "axiom", | |
"version": "1.0.0", | |
"description": "basic", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Khizar Manzoor", | |
"license": "ISC", | |
"dependencies": { | |
"cheerio": "^0.20.0", | |
"express": "^4.14.0", | |
"q": "^1.4.1", | |
"request": "^2.74.0" | |
} | |
} |
This file contains hidden or 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
'use strict' | |
var q = require("q"); | |
var request_external = require("request"); | |
var cheerio = require("cheerio"); | |
var express = require('express'); | |
var router = express.Router(); | |
const NO_RESPONSE_MESSAGE = "NO RESPONSE"; | |
// middleware that is specific to this router | |
router.use((req, res, next) => { | |
console.log('Time: ', Date.now()); | |
req.requestTime = Date.now(); | |
next(); | |
}); | |
router.get('/', (request, response) => { | |
var query_params = request.query; | |
var new_response_obj = {}; | |
if(query_params.hasOwnProperty('address')) | |
{ | |
var addresses = query_params['address']; | |
var promise_arr = []; | |
for(var i = 0, len = addresses.length; i < len; i++) | |
{ | |
var url = validate_url(addresses[i]) | |
promise_arr.push(get_title(url)); | |
} | |
q.all(promise_arr).then( values => { | |
for(var i = 0, len = values.length; i < len; i++) { | |
values[i] = '<li>' + values[i] + '</li>'; | |
} | |
new_response_obj.data = values; | |
response.send(new_response_obj); | |
}); | |
} | |
// res.send('dir 1 Birds home pasge'); | |
}); | |
function get_title(url) | |
{ | |
var defer = q.defer(); | |
request_external(url, {method: 'GET'}, function(error, response, body) { | |
var title = ''; | |
if(response) { | |
if(response.statusCode === 200) { | |
title = extract_title_from_html(body); | |
if(title) | |
{ | |
title = url + ' - ' + title; | |
defer.resolve(title); | |
} | |
else | |
{ | |
title = url + ' - ' + NO_RESPONSE_MESSAGE; | |
defer.resolve(title); | |
} | |
} | |
else | |
{ | |
title = url + ' - ' + NO_RESPONSE_MESSAGE; | |
defer.resolve(title); | |
} | |
} | |
else | |
{ | |
title = url + ' - ' + NO_RESPONSE_MESSAGE; | |
defer.resolve(title); | |
} | |
}); | |
return defer.promise; | |
} | |
function validate_url(url) | |
{ | |
if(url.indexOf('http') || url.indexOf('https') == -1) | |
{ | |
url = 'http://' + url; | |
} | |
return url; | |
} | |
function extract_title_from_html(html_str) | |
{ | |
var $ = cheerio.load(html_str), | |
title = $("title"); | |
title = title.html(); | |
return title; | |
} | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment