-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
66 lines (53 loc) · 1.69 KB
/
app.js
File metadata and controls
66 lines (53 loc) · 1.69 KB
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
const axios = require('axios');
const express = require('express');
const app = express();
const PORT = process.env.PORT || 5000;
const path = require('path');
const handlebars = require('express-handlebars');
app.engine('.hbs', handlebars({ extname: '.hbs' }));
app.set("PORT", PORT);
app.use(express.static(path.join(__dirname, 'assets')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', '.hbs');
app.get('/', function (req, res) {
let url = `https://indreed.herokuapp.com/api/jobs?q=web+developer&limit=50`;
axios({
method: 'get',
url
})
.then(function (response) {
let jobs = response.data;
res.render("index", { title: "Job Scraper", jobs: jobs});
})
.catch(function (error) {
console.log(error);
});
});
const expressip = require('express-ip');
app.use(expressip().getIpInfoMiddleware); // registering the middleware
app.get('/search', function (req, res) {
const queries = req.query;
var ipInfo = req.ipInfo
if (ipInfo.country)
url = `https://indreed.herokuapp.com/api/jobs?country=${ipInfo.country}`;
else
url = `https://indreed.herokuapp.com/api/jobs`;
if (queries){
axios.get(url, {
params: queries
})
.then(function(response){
res.render("search", { title: "Job Scraper", jobs: response.data, countryCode: ipInfo.country});
})
.catch(function(error) {
console.log(error);
});
}
else {
res.render("search", {title: "Job Scraper"})
}
});
app.listen(app.get('PORT'), function () {
console.log('Express started on http://localhost:' +
app.get('PORT') + '; press Ctrl-C to terminate.');
});