An overview of the

Articles are mainly copied from others, mainly for records.

First put big guy article address:

Build an SSR application with VUE-CLI3

Based on vuE-CLI3 SSR program to achieve hot update function

What can I say? I’m a little obsessed with SSR. First of all, we know that SSR is actually part M of the early MVC era, when templates were developed. But in the era of front and back end separation, SSR became a more complicated thing. The main reason is that SSR now comes with templates because of react, Vue and other frameworks. This is not available if you use it directly on the server.

Then LAST year, I repeatedly built SSR, such as NUxT, EggJS esayWebPack and so on. However, I often wonder whether I should build my own or use a framework like NUXT if I need to use SSR for the most core project of my company.

Nuxt and other frameworks have a fatal point, we need tools like PM2 to ensure the performance of multithreading. In my earlier use of EGGJS, PM2 was not required. Eggjs also supports multi-threading startup and experienced double Eleven. So I really wanted to merge SSR and EGGJS together. But I still want to do it myself.

Read the official SSR document: ssr.vuejs.org/zh/ for many times, each time is in we…

I finally copied this article.

process

This I do not want to outline, mainly copy the big guy. The author’s process of the complete down is completely possible.

It’s basically two pits

1, error about asterisk route in koA-router section

router.get("*", handleRequest);
Copy the code

I changed it to /

2. An error occurs during startup during vue. Config modification.

Optimization does not exist, and will not return an error when added

optimization: {
	      splitChunks: TARGET_NODE ? false : undefined,
	    },
	    plugins: [TARGET_NODE ? new VueSSRServerPlugin() : new VueSSRClientPlugin()],
Copy the code

3, personal modification of the git address: github.com/ht-sauce/vu…

How to verify their successful transformation?

After looking at the big guy’s transformation successfully running up, I actually have a little square, do not know whether it is successful.

Let’s talk about baidu and other search engine principles. The idea is that a crawler will request your address, retrieve your internal HTML page, and then analyze your content based on the HTML page.

Then why can’t SPA get. Here’s an example:

<! DOCTYPE html> <html lang=en> <head> <meta charset=utf-8> <meta http-equiv=X-UA-Compatible content="IE=edge"> <meta name=viewport content="width=device-width,initial-scale=1"> <link rel=icon href=/dve/favicon.ico> <link rel=stylesheet Type = "text/CSS href = / dve/element - the UI / 2.13.0 / index. The CSS > < script SRC = / dve vue / 2.6.11 vue. Min. Js > < / script > < script SRC = / dve/element - the UI / 2.13.0 / index, js > < / script > < script SRC = / dve/vuex / 3.0.1 / vuex js > < / script > < script SRC = / dve/wavesurfer/wavesurfer. Js > < / script > < title > number one video editor < / title > < link href = / dve/CSS/digital. 6 f1b1310. CSS rel=prefetch> <link href=/dve/js/digital.90f990ef.js rel=prefetch> <link href=/dve/css/app.4434a726.css rel=preload as=style> <link href=/dve/js/app.1936d768.js rel=preload as=script> <link href=/dve/js/chunk-vendors.919914d8.js rel=preload as=script> <link href=/dve/css/app.4434a726.css rel=stylesheet> </head> <body> <noscript><strong> Please allow the browser to execute JavaScript</strong></noscript> <div id=app></div> <script src=/dve/js/chunk-vendors.919914d8.js></script> <script src=/dve/js/app.1936d768.js></script> </body> </html>Copy the code

This is what you get from a search engine. You can get a hammer for that.

The search engine is going to get something like CSDN

<! DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <link rel="canonical" href="https://blog.csdn.net/liumiaocn/article/details/108267949"/> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <meta name="renderer" content="webkit"/> <meta name="force-rendering" content="webkit"/> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/> <meta name="viewport" content="width=device-width, Initial - scale = 1.0, the minimum - scale = 1.0, the maximum - scale = 1.0, user-scalable=no"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta name="report" content='{"pid": "blog", <meta http-equiv=" cache-control "content=" no-siteApp "> <meta http-equiv=" cache-control" content="no-siteapp" /> <meta name="referrer" content="always"> <link rel="alternate" media="handheld" href="#" /> <meta name="shenma-site-verification" content="5a59773ab8077d4a62bf469ab966a63b_1497598848"> <meta name="csdn-baidu-search" Content ='{"autorun":true,"install":true,"keyword":" cloud IDE: Eclipse Che: The Eclipse IDE next generation "} "> < link href =" https://csdnimg.cn/public/favicon.ico "rel =" SHORTCUT ICON "> < title > cloud IDE: Eclipse Che: </title> <meta name="keywords" content=" cloud IDE: Eclipse Che: "> <meta name="description" content="Eclipse Che is the next generation OF IDE. In this article we find out." > < script SRC = "/ / right sdnimg. Cn/tingyun / 1.8.3 / blog. Js' type = 'text/javascript' > < / script > < link rel =" stylesheet" href="https://csdnimg.cn/release/phoenix/template/new_css/detail_enter-54d3fd4be5.min.css"> <script type="application/ld+json">{"@context":"https:\/\/ziyuan.baidu.com\/contexts\/cambrian.jsonld","@id":"https:\/\/blog.csd n.net\/liumiaocn\/article\/details\/108267949","appid":1638831770136827,"title":"\u4e91IDE\uff1aEclipse Che\uff1aEclipse\u4e0b\u4e00\u4ee3IDE_\u77e5\u884c\u5408\u4e00 \u6b62\u4e8e\u81f3\u5584-CSDN\u535a\u5ba2","pubDate":"2020-08-28T07:48:16","upDate":"2020-08-28T07:48:16"}</script> <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/themes_skin/skin-number/skin-number-01fe94d91c.min.css"> <! - custom skin style -- -- > < script SRC = "https://csdnimg.cn/public/common/libs/jquery/jquery-1.9.1.min.js" type="text/javascript"></script> <! - js reference -- > < script SRC = "/ / right sdnimg. Cn /?? Fixed - sidebar / 1.1.6 / fixed - sidebar. Js, report / 1.5.6 / report. Js." " type="text/javascript"></script> <link rel="stylesheet" Href = "https://csdnimg.cn/public/sandalstrap/1.4/css/sandalstrap.min.css" > < style >. MathJax. MathJax_Message, .MathJax_Preview{ display: none } </style> </head> <body class="nodata " > <link rel="stylesheet" href="https://csdnimg.cn/public/common/toolbar/content_toolbar_css/content_toolbar.css"> <script id="toolbar-tpl-scriptId" src="https://csdnimg.cn/public/common/toolbar/js/content_toolbar.js" type="text/javascript" domain="https://blog.csdn.net/"></script> <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/new_css/blog_code-c3a0c33d5c.css"> <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/vendor/pagination/paging-e040f0c7c8.css"> <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/new_css/chart-3456820cac.css" /> <div class="main_father clearfix d-flex justify-content-center" style="height:100%;" > <div class="container clearfix" id="mainBox"> <aside class="blog_container_aside"> <! <div class="avatar-box d-flex"> <div class="avatar-box d-flex justify-content-center flex-column"> <a href="https://blog.csdn.net/liumiaocn" Data - report - click = '{" mod ":" popu_379 ", "the SPM" : "1001.2101.3001.4121", "dest" : "https://blog.csdn.net/liumiaocn", "ab", the "new"} "> <img src="https://profile.csdnimg.cn/5/1/1/3_liumiaocn" class="avatar_pic" username='liumiaocn'> </a> </div> <div class="user-info d-flex flex-column profile-intro-name-box"> <div> <a href="https://blog.csdn.net/liumiaocn" class="" Id =" id" id=" id" Data - report - click = '{" mod ":" popu_379 ", "the SPM" : "1001.2101.3001.4122", "dest" : "https://blog.csdn.net/liumiaocn", "ab", the "new"} "> <span class="name "username='liumiaocn'> </span> </a> <span></span> <span class="flag expert-blog"><img Class = "identity" SRC = "https://csdnimg.cn/identity/expert.png" Alt = "" > < span class =" mercifully "> blog CSDN certification experts < / span > < / span > < span Class ="flag company-blog"><span class="bubble">CSDN certified enterprise blog </span></span> </div> <div Class =" profile-intro-name-boxfooter ">< span class="personal-home-page">< span class="personal-home-page">< span class="personal-home-page">< span class="personal-home-page"><a class="personal-home-certification" href="https://me.csdn.net/liumiaocn?utm_source=14998968" target="_blank"><img SRC = "https://csdnimg.cn/identity/nocErtification.png" Alt = "" > no certification < / a > < / span > < / div > < / div > < / div >Copy the code

So it’s going to look something like this, and you can see that your HTML content is going to have HTML tags and things like that in addition to the head.

Note that this is a request, not one where you first request the page path, then render the page via JS. Remember, this is what the server gave the browser on the first request

Give me the simplest crawler and try it yourself

This is actually very simple, in fact, you can also use ordinary Ajax request to try. As long as the content you request is the first time with HTML tags and other key information, then it is successful.

Here is the code for the Express section

Installation is basically

npm i express superagent cheerio
Copy the code

SuperAgent, used by the server to request address data

Cheerio is a minimal jQuery, but it can be used on the server

const express = require('express'); // Call the express instance, which is a function that, when called without arguments, returns an express instance and assigns that variable to the app variable. const superagent = require('superagent'); const cheerio = require('cheerio'); const app = express(); app.get('/', (req, res, next) => { superagent.get('http://localhost:3000') .end((err, Sres) => {// General error handling if (err) {return next(err); } // sres.text contains the HTML content of the web page, passing it to cheerio.load to get a variable that implements the jquery interface, Let $= cheerio.load(sres.text); $= cheerio.load(sres.text); let items = sres.text; console.log(sres.text) /*$('.item_title a').each((idx, element) => { let $element = $(element); items.push({ title: $element.text(), href: $element.attr('href') }); }); */ res.send(items); }); }); app.listen(5000, function () { console.log('app is listening at port 5000'); });Copy the code

The last

Nothing in the end, just to express myself this time the article is rather sloppy.