如何直接从Express中的文件夹提供静态资产
在图片中包含图片,CSS等内容是很常见的public
子文件夹,并将其公开到根目录:
const express = require('express')
const app = express()
app.use(express.static(‘public’))
/* … */
app.listen(3000, () => console.log(‘Server ready’))
如果您有index.html
归档在public/
,如果您现在点击根域网址(http://localhost:3000
)
免费下载我的Express.js手册
更多速成教程:
- Express,流行的Node.js框架
- 使用Express检索GET查询字符串参数
- 使用express-validator验证Express中的输入
- 快递模板
- 使用Express服务静态资产
- 使用Express发送JSON响应
- 快速会议
- 使用Express发送回复
- 使用Express发送文件
- 使用Express-Validator清理Express中的输入
- 在Express中路由
- 具有自签名证书的Express HTTPS服务器
- Express,请求参数
- 使用Express检索POST查询参数
- 使用Express处理重定向
- 快速中间件
- 设置让我们为Express加密
- 在Express中使用HTTP标头
- 在Express中处理表格
- 使用Express处理表单中的文件上传
- 在Express中处理CORS
- 使用Express管理Cookies