-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
146 lines (127 loc) · 3.74 KB
/
app.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var express = require("express");
var app = express();
var fs = require("fs");
var path = require("path");
//2021.01.13 로그인을 위한 session 추가
var session = require("express-session");
//body-parser : FOR HANDLING POST DATA
const bodyParser = require("body-parser");
//**https
//https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener
//https://eunsukimme.github.io/nodejs/2019/09/20/Express-SSL-HTTPS/
const https = require("https");
/*
TODO: GET SSL
const options = {
key: fs.readFileSync(__dirname + '/인증서경로/domain_xxxxx.key.pem')
cert: fs.readFileSync(__dirname + '/인증서경로/domain_xxxxx.crt.pem')
ca: fs.readFileSync(__dirname + '/인증서경로/ca-chain-bundle.pem')
};
const options = {
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
const options = {
pfx: fs.readFileSync('test/fixtures/test_cert.pfx'),
passphrase: 'sample'
};
*/
//axios : get html from url
//cheerio : use queryselector
//npm install --save axios cheerio
const axios = require("axios");
const cheerio = require("cheerio");
//selenium
const { Builder, By, Key, until } = require("selenium-webdriver");
//node-schedule
//매일 자정에 scraping작업을 한다.
//reference : https://bblog.tistory.com/307
var schedule = require("node-schedule");
const batch = require("./batch.js");
var scheduler = schedule.scheduleJob("00 00 00 * * *", async function () {
await batch.scraping();
});
//port set
const port = process.env.PORT || 3000;
//ejs init
app.set("views", __dirname + "/views");
app.set("view engine", "ejs");
app.engine("html", require("ejs").renderFile);
//use public directory
app.use(express.static(path.join(__dirname, "/public")));
//use session 2021.01.13 로그인을 위해 session 추가
app.use(
session({
secret: "root##3804",
resave: false,
saveUninitialized: true,
})
);
// CORS
app.all("/*", (req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
//use body-parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//run server
app.listen(port, function () {
console.log("Server is running on port " + port);
});
/*
TODO : GET SSL
https.createServer(options, app).listen(port, function () {
console.log("Server is running on port " + port);
});
*/
/*
의문점 : selenium을 활용하려면 webdriver가 필요한데,
이거 heroku에서 세팅 가능한지? putty같은걸로 접속 되는지 확인해보기
https://devcenter.heroku.com/articles/heroku-cli-commands
*/
/******* DATABASE CONNECTION START *******/
/*
node js - pg => pool, client 차이는?
spring에서 db connection pool 사용하는 이유와 같다.
재접속 필요없으므로
https://node-postgres.com/features/pooling
*/
//db setting
require("./db.js").connect();
//for testing body-parser and express-session
/*
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(session({
secret: '@#@$MYSIGN#@$#$',
resave: false,
saveUninitialized: true
}));
//for what?
var router = require('./router/main')(app, fs);
*/
/*
routing method... what is better method??
//assigning route function into module.exports.....why?
module.exports = function(app, fs) {
app.get("/", function(req, res) {
res.render("index", {
title: "hello world",
length: 123
});
});
}
*/
/*
Selenium
npm install --save selenium-webdriver
selenium documentation
https://www.selenium.dev/documentation/en/getting_started_with_webdriver/locating_elements/
*/
require("./router.js").route(app);