Reasons to choose leanCloud
1, temporarily too lazy to write their own, such as the follow-up to write their own time to migrate; 2. Baidu Statistics can only obtain 2000 statistical results per week; 3. The data API of leanCloud is free to use 3W times per day, that is, write and read twice to obtain a statistical result. As long as the daily PV does not exceed 1.5W, it can be free and fully sufficient.
Create a leanCloud storage table
Create the app first:
Then I go into storage, create a table called Counter, add key,time, index key, not important data, so I make it read and write for everyone, okay
Add a row with a key value of total as the historical access statistics
Node Server access
Each application has its appId and appKey. Before reading or writing data, the appId and appKey must be used to initialize data.
Part of the code is as follows:
import * as AV from 'leancloud-storage'; import * as config from 'config'; import * as R from 'ramda'; // Read appId and appKey av.init (config.get('leanCloud')) from config file; const Counter = AV.Object.extend('Counter'); /** * async function incrementTotal() {try {const query = new av.query (Counter); const totalData = (<any> await query.equalTo('key', 'total').find())[0]; const total = AV.Object.createWithoutData('Counter', totalData.id); total.increment('time', 1); const data = <any> await total.save(null, { fetchWhenSave: true }); Return r.objof ('total', data.attributes. Time); } catch(e) { console.log(e); }}Copy the code
In the above code, appId and appKey are read from the configuration file. The configuration of the Production environment will not be submitted to Github to avoid leakage. When the page is accessed, call incrementTotal() and get the latest number of visits, which is injected into the page global variable window.__config__ and set in footer.tsx componentDidMount
componentDidMount() {
const PV = (window as any).__CONFIG__.PV;
this.setState({ PV });
}Copy the code
conclusion
There are many points involved in the whole series. Since I rarely wrote articles in the past, I may not be organized enough. If you have any questions, you can contact me via QQ or email, and I will find time to answer them. This blog has no time to add comment function, I will find time to add it later. PS: The blog was finally deployed on Ali Cloud ECT. The 2GB 1 core was bought at a 20% discount for 3 years, with an average of 500+ a year.
Source address: github.com/hihl/blog