From 9d7c0f601fb225ee7e9cf1d0e8f095621cdfbbf6 Mon Sep 17 00:00:00 2001 From: Zephyr Date: Sun, 20 Feb 2022 19:23:35 +0800 Subject: [PATCH] fix: can't get the CSRF token Root cause: Use the research API can't get CSRF. It will raise the below error: ``` yarn run v1.22.10 $ node app.js /Users/zephyr/Documents/Coding/dummy/rentHouse/lib/getToken.js:9 const csrfToken = regExp.exec(response.body)[1]; ^ TypeError: Cannot read properties of null (reading '1') at module.exports (/Users/zephyr/Documents/Coding/dummy/rentHouse/lib/getToken.js:9:47) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async module.exports (/Users/zephyr/Documents/Coding/dummy/rentHouse/lib/getFirstPostId.js:6:22) at async /Users/zephyr/Documents/Coding/dummy/rentHouse/app.js:11:22 Node.js v17.5.0 error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. ``` Solution: change the fetching site to the 591 index URL. --- app.js | 2 +- lib/getFirstPostId.js | 2 +- lib/getToken.js | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index 3d88199..f57a5a5 100644 --- a/app.js +++ b/app.js @@ -11,7 +11,7 @@ let countFail = 0; let originPostId = await getFirstPostId(); stopIntervalId = setInterval(async () => { console.log(`${new Date()}: '我還活著'`); - const headerInfo = await getToken(process.env.TARGET_URL); + const headerInfo = await getToken(); const houseListURL = `https://rent.591.com.tw/home/search/rsList?${process.env.TARGET_URL.split('?')[1]}`; const csrfToken = headerInfo[0]; const cookie = headerInfo[1]; diff --git a/lib/getFirstPostId.js b/lib/getFirstPostId.js index 23709d9..8e23ae3 100644 --- a/lib/getFirstPostId.js +++ b/lib/getFirstPostId.js @@ -3,7 +3,7 @@ const getToken = require('./getToken'); require('dotenv').config(); module.exports = async () => { - const headerInfo = await getToken(process.env.TARGET_URL); + const headerInfo = await getToken(); const houseListURL = `https://rent.591.com.tw/home/search/rsList?${process.env.TARGET_URL.split('?')[1]}`; const csrfToken = headerInfo[0]; const cookie = headerInfo[1]; diff --git a/lib/getToken.js b/lib/getToken.js index 512ecf7..52bec84 100644 --- a/lib/getToken.js +++ b/lib/getToken.js @@ -1,9 +1,9 @@ const { getRequest } = require('./request'); -module.exports = async (targetUrl) => { +module.exports = async () => { const regExp = new RegExp('', 'gi'); const response = await getRequest({ - url: targetUrl, + url: 'https://rent.591.com.tw', json: true, }); const csrfToken = regExp.exec(response.body)[1];