node.js基于express模拟客户端渲染和服务端渲染(基础无模板)
2021/11/12 14:10:05
本文主要是介绍node.js基于express模拟客户端渲染和服务端渲染(基础无模板),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
服务端渲染
/* * @Description:express路由表 * @Author: NanKe * @Date: 2021-11-08 16:07:49 * @LastEditTime: 2021-11-12 12:26:42 * @LastEditors: NanKe * @Cnblogs: https://www.cnblogs.com/NanKe-Studying/ * @FilePath: \node\express\router\index.js */ const express = require("express") const router = express.Router() const { list } = require("../controllers") router.get('/api/list', list) module.exports = router
/* * @Description:控制层 * @Author: NanKe * @Date: 2021-11-08 21:08:11 * @LastEditTime: 2021-11-08 21:14:14 * @LastEditors: NanKe * @Cnblogs: https://www.cnblogs.com/NanKe-Studying/ * @FilePath: \node\express\controllers\index.js */ const list = (req, res, next) => { let data = '<ul>' for (let i = 0; i < 100; i++) { data += `<li>${i}</li>` } data += '</ul>' res.send(data) } exports.list = list
<!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.0"> <title>static</title> <link rel="stylesheet" href="./styles/reset.css"> <script src="./scripts/common.js"></script> </head> <body> hello <img src="./images/img1.jpg" alt=""> <a href="/api/list">api/list</a> </body> </html>
/* * @Description:express服务端渲染 * @Author: NanKe * @Date: 2021-11-08 16:11:51 * @LastEditTime: 2021-11-12 12:20:17 * @LastEditors: NanKe * @Cnblogs: https://www.cnblogs.com/NanKe-Studying/ * @FilePath: \node\express\express2.js */ const express = require("express") const router = require("./router/index") const app = express() //处理前端传递表单数据 parse application/x-www-form-urlencoded app.use(express.urlencoded({ extended: false })) //处理前端传递json数据 parse application/json app.use(express.json()) //静态资源服务中间件 app.use(express.static('./public')) app.use('/', router) app.listen(8080, () => { console.log("localhost:8080 Listen"); })
目录结构
点击访问
成功请求服务端渲染好的html模板
客户端渲染(前后端分离)
/* * @Description:express路由表 * @Author: NanKe * @Date: 2021-11-08 16:07:49 * @LastEditTime: 2021-11-12 12:26:42 * @LastEditors: NanKe * @Cnblogs: https://www.cnblogs.com/NanKe-Studying/ * @FilePath: \node\express\router\index.js */ const express = require("express") const router = express.Router() const { list } = require("../controllers") router.get('/api/list', list) module.exports = router
/* * @Description:控制层 * @Author: NanKe * @Date: 2021-11-08 21:08:11 * @LastEditTime: 2021-11-12 13:07:38 * @LastEditors: NanKe * @Cnblogs: https://www.cnblogs.com/NanKe-Studying/ * @FilePath: \node\express\controllers\index.js */ const list = (req, res, next) => { let dataObj = { code:200, data:[] } for(let i=0;i<100;i++){ dataObj.data.push('line'+i) } res.send(dataObj) } exports.list = list
/* * @Description: * @Author: NanKe * @Date: 2021-11-12 11:43:54 * @LastEditTime: 2021-11-12 13:39:55 * @LastEditors: NanKe * @Cnblogs: https://www.cnblogs.com/NanKe-Studying/ * @FilePath: \node\express\public\scripts\common.js */ $.ajax({ url: "/api/list", success(result) { console.log(result); let html = '<ul>' $.each(result.data, function (indexInArray, valueOfElement) { html += '<li>' + valueOfElement + '</li>' }); html +="</ul>" $('#list').html(html) } });
/* * @Description:express+路由表--》获取前端传递的参数 * @Author: NanKe * @Date: 2021-11-08 16:11:51 * @LastEditTime: 2021-11-12 12:20:17 * @LastEditors: NanKe * @Cnblogs: https://www.cnblogs.com/NanKe-Studying/ * @FilePath: \node\express\express2.js */ const express = require("express") const router = require("./router/index") const app = express() //处理前端传递表单数据 parse application/x-www-form-urlencoded app.use(express.urlencoded({ extended: false })) //处理前端传递json数据 parse application/json app.use(express.json()) //静态资源服务中间件 app.use(express.static('./public')) app.use('/', router) app.listen(8080, () => { console.log("localhost:8080 Listen"); })
<!-- * @Description: * @Author: NanKe * @Date: 2021-11-12 11:43:54 * @LastEditTime: 2021-11-12 13:32:40 * @LastEditors: NanKe * @Cnblogs: https://www.cnblogs.com/NanKe-Studying/ * @FilePath: \node\express\public\index.html --> <!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.0"> <title>static</title> <link rel="stylesheet" href="./styles/reset.css"> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script> <script src="./scripts/common.js"></script> </head> <body> hello <img src="./images/img1.jpg" alt=""> <a href="/api/list">api/list</a> <div id="list"></div> </body> </html>
可以看到这种请求方式 就是前端获取到json数据 html才在浏览器进行渲染
这篇关于node.js基于express模拟客户端渲染和服务端渲染(基础无模板)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-16Vue3资料:新手入门必读教程
- 2024-11-16Vue3资料:新手入门全面指南
- 2024-11-16Vue资料:新手入门完全指南
- 2024-11-16Vue项目实战:新手入门指南
- 2024-11-16React Hooks之useEffect案例详解
- 2024-11-16useRef案例详解:React中的useRef使用教程
- 2024-11-16React Hooks之useState案例详解
- 2024-11-16Vue入门指南:从零开始搭建第一个Vue项目
- 2024-11-16Vue3学习:新手入门教程与实践指南
- 2024-11-16Vue3学习:从入门到初级实战教程