编程有意思

通知聚合服务

我把各个平台的通知汇聚在一起,显示在 Kindle 上,然后摆在桌面。

但这个东西不适合分享出来,这有很多原因,这里不解释了。但是自己书写一个也挺简单的,只不过两个知识点:

  • 前端 JS 不方便跨域获取数据,所以我们需要一个服务器来请求数据
  • 服务器就是构造一个 get 方法(多数时候都是 get)而已

用 Node.js 引入 http(内置模块,无需额外安装),

const http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.write('要输出的内容')
    response.end();
}).listen(8080);

console.log('Server running at http://127.0.0.1:8080/');

构造请求也用 http 这个模块,不过有些请求需要带上 cookie。也可以使用 superagent,格式上可能简单点。

const req = http.request({
  method: 'GET',
  host: 'https://website.com',
  port: 80,
  path: '/api',
  headers: {
    Cookie: ['a=111', 'b=222']
  }
}, res => {
  res.on('data', data => console.log(data.toString()) );
});
req.on('error', function (e) { 
  console.log('problem with request: ' + e.message); 
});
req.end();

然后,然后就把两者缝合一下就行了,展示页面随便写写就好,反正服务器是自己的,展示页面疯狂轮询自己的后端没所谓啦,如果设备支持 websocket 的话可以不轮询(Kindle 似乎不支持)。

服务器这边请求别人的数据就克制点,五分钟十分钟的查询一次就好。