-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscraper.js
More file actions
50 lines (41 loc) · 1.38 KB
/
scraper.js
File metadata and controls
50 lines (41 loc) · 1.38 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
var express = require('express');
var fs = require('fs');
var app = express();
var http = require('http');
var bodyParser = require('body-parser');
//necessary to circumvent Same-origin policy - DON'T TOUCH THIS PART
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
};
app.use(allowCrossDomain);
//This allows us to properly parse data from the request (TODO: do we know why?)
app.use(bodyParser());
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/client');
app.use(express.static(__dirname + '/client'));
app.get('/', function(req, res) {
res.render("index");
});
app.post('/', function(req, res) {
//can we make the txt file dynamically update based on the inputs in index?
fs.appendFile('scraped_data/items-1-n.txt', JSON.stringify(req.body) + '\n', function(err, data) {
if (err) {
console.log(err);
console.log("ID NUMBER: " + req.body.id);
res.send(404, err);
} else {
res.send(200,'sent');
}
});
});
console.log('listening on 8000');
app.listen(8000);