1.本地运行
在安装好docker之后, 我们先全局安装rendertron:
npm install -g rendertron
然后我们需要安装谷歌浏览器(作为合格的开发都应该有谷歌浏览器~),然后就可以用它的cli来启动服务了,我们只需要在命令行执行如下命令:
rendertron 复制代码
之后控制台会打印本地服务启动的地址,比如localhost:3000 这个时候我们只需要在地址后面输入我们想渲染的网站即可: localhost:3000:render/你的网站地址,
如下图所示:
此时我们的rendertron服务已经搭建完成, 接下来我们可以在服务端来实现ssr了,代码如下:
const koa = require('koa');
const app = new koa();
app.use(async (ctx, next) => {
ctx.type = "html";
if(/Baiduspider\/2\.0/g.ctx.header['user-agent']) {
// 是百度爬虫,则转发到rendertron服务中
ctx.redirect(`http://localhost:3000/render/${ctx.url}`)
}else {
// 渲染正常的路由页面
}
await next();
})
app.listen('80');
当然如果我们后端技术栈采用的是express, rendertron有专门的中间件可以使用, 不仅仅可以拦截百度的爬虫,具体用法如下:
const express = require('express');
const rendertron = require('rendertron-middleware');
const app = express();
app.use(rendertron.makeMiddleware({
proxyUrl: 'http://your-rendertron-instance/render',
}));
// 正常的路由和页面渲染逻辑
app.use(...);
app.listen(81);