-
express
-
express web服务器 > 后端渲染
-
原生
-
静态服务器
/* 使用 express 搭建静态服务器 静态服务器 后端语言 + 后端渲染模板 生成前端的html结构,然后在发送到前台 上面这种做法就叫做后端渲染 案例: php java + jsp Node + html/ejs/pug ---> html结构 --》 前台 */ const express = require( 'express' ) //得到app对象, 目的是为了绑定中间件 const app = express() const PORT = 8000 const HOST = 'localhost' const fs = require( 'fs' ) // 文件系统 // 要想将模板中的内容发送出去,我们可以通过app对象和路由中间件来做 app.get('/home',( request,response,next ) => { // fs.readFile( 路径,字符编码, 回调函数) fs.readFile( './static/html/index.html', 'utf8', ( error,docs ) => { if( error ){ console.log( error ) }else{ response.send( docs ) } }) }) // 监听服务器 app.listen( PORT,HOST,() => { console.log( `服务器运行在: http://${ HOST }:${ PORT }` ) })
-
api服务器
/* 使用express 打造api服务器 在api服务器中,一个路由就是一个api,也就是一个后端接口 接口测试 情景: 某一天,一个后端找你,然你帮他测接口 含义: 让你发ajax请求他的接口,看数据是否正常拿到 跨域: 前端跨域: jsonp 反向代理 框架里面用 后端跨域 设置请求头 一行 cors中间件 情况: 使用测试工具测试 */ const express = require( 'express' ) const app = express() //得到app对象 const PORT = 5000 const HOST = 'localhost' // 引入user路由模块 const userRouter = require<
-
使用express-generator完成前后端不分离的的部分步骤
最新推荐文章于 2024-03-28 19:39:47 发布