关于SSR的概念和其优缺点在之前的博客Node服务器端渲染(SSR概念篇)也做过总结,目前学习了如何在实际的代码中实现SSR的前端项目搭建,写此博客也是对 SSR+前端框架 项目工程搭建的一个总结,加上个人的理解。
旨在讲个大概的 代码 结构,基本的结构和代码是用来干什么的,不能做到从 0 基础 到 完全完成项目的初始化。
没有用到 Nuxt.js 或者 Next.js 这种SSR框架,原生 node+vue 讲解
Node.js + Express + Vue3
node环境
SSR的实现重点是在 服务端 生成页面的元素 DOM等返回给 浏览器 。
这里我们嵌入前端框架实现SSR,大概分下面几个步骤:
build是打包文件,本项目使用webpack打包工具进行的项目打包
咱们按照"核心实现原理"步骤中的几步中,来进行项目结构,功能的介绍src下的项目核心SSR配置:
用前端框架编写app.js,引入框架的组件
import { createSSRApp } from "vue";
import App from "./App.vue";
export default function createApp() {
// 创建app
const app = createSSRApp(App);
return app;
}
// createApp(App).mount("#app");
App
count: {{ count }}
import express from "express";
import createApp from "../app";
import { renderToString } from "@vue/server-renderer";
let app = express();
let App = createApp();
// 部署 静态资源
app.use(express.static("build"));
// 和注册的顺序有关系,优先匹配第一个符合的路径
app.get("/*", async (req, res) => {
const AppContent = await renderToString(App);
console.log(AppContent);
res.send(`
Document
Vue + Server Side Render
${AppContent}
`);
});
app.listen(3000, () => {
console.log("Node Server Start On 3000");
});
import { createApp } from "vue";
import App from "../App.vue";
let app = createApp(App);
app.mount("#app"); // 需要在body添加id=app标签
let path = require("path");
let nodeExternals = require("webpack-node-externals");
let { DefinePlugin } = require("webpack");
let { VueLoaderPlugin } = require("vue-loader/dist/index");
module.exports = {
mode: "development",
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: "babel-loader",
},
],
},
{
test: /\.vue$/,
loader: "vue-loader",
},
],
},
plugins: [
new VueLoaderPlugin(),
// 定义两个环境变量,关闭option api 和 pro 调试的提示
new DefinePlugin({
__VUE_OPTIONS_API__: false,
__VUE_PROD_DEVTOOLS__: false,
}),
],
resolve: {
extensions: [".js", ".json", ".wasm", ".jsx", ".jsx", "vue"],
},
};
let path = require("path");
let baseConfig = require("./base.config");
let { merge } = require("webpack-merge");
module.exports = merge(baseConfig, {
target: "web", // 构建目标环境
entry: "./src/client/index.js",
output: {
filename: "client_bundle.js",
path: path.resolve(__dirname, "../build/client"),
},
});
let path = require("path");
let nodeExternals = require("webpack-node-externals");
let baseConfig = require("./base.config");
let { merge } = require("webpack-merge");
module.exports = merge(baseConfig, {
target: "node",
entry: "./src/server/index.js",
output: {
filename: "server_bundle.js",
path: path.resolve(__dirname, "../build/server"),
},
externals: [nodeExternals()],
});
{
"name": "01-node-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build:server": "webpack --config ./config/server.config.js --watch",
"build:client": "webpack --config ./config/client.config.js --watch",
"start:server": "nodemon ./build/server/server_bundle.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.2.1",
"express": "^4.18.2",
"nodemon": "^2.0.20",
"pinia": "^2.0.26",
"vue": "^3.2.45",
"vue-loader": "^17.0.1",
"vue-router": "^4.1.6",
"webpack-node-externals": "^3.0.0"
},
"devDependencies": {
"@babel/preset-env": "^7.20.2",
"babel-loader": "^9.1.0",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.0",
"webpack-merge": "^5.8.0"
}
}