-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp copy.js
107 lines (84 loc) · 2.95 KB
/
app copy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/////////////////////////////
//// server side ////
///////////////////////////
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(express.static('public'));
app.get('/index.htm', function (req, res) {
res.sendFile( __dirname + "/" + "index.htm" );
})
app.post('/process_post', urlencodedParser, function (req, res) {
// Prepare output in JSON format
response = {
first_name:req.body.first_name,
last_name:req.body.last_name
};
console.log(response);
res.end(JSON.stringify(response));
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
//-----------------------------------------------------------------------------------
// var routes = require('./routes');
// var user = require('./routes/user');
// dependencies
var express = require('express');
var http = require('http');
var path = require('path');
var request = require('request');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
// first route
app.get('/', function(req, res) {res.render('index')});
// second route
app.get('/searching', function(req, res){
// input value from search
var val = req.query.search;
//console.log(val);
// url used to search yql
var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20craigslist.search" +
"%20where%20location%3D%22sfbay%22%20and%20type%3D%22jjj%22%20and%20query%3D%22" + val + "%22&format=" +
"json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
// console.log(url);
// request module is used to process the yql url and return the results in JSON format
request(url, function(err, resp, body) {
body = JSON.parse(body);
// logic used to compare search results with the input from user
// console.log(!body.query.results.RDF.item['about'])
if (!body.query.results.RDF.item) {
craig = "No results found. Try again.";
} else {
results = body.query.results.RDF.item[0]['about']
craig = '<a href ="'+results+'">'+results+'</a>'
}
// pass back the results to client side
res.send(craig);
});
// testing the route
// res.send("WHEEE");
});
// old routes
// app.get('/', routes.index);
// app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});