当前位置:网站首页>1-14 express托管静态资源
1-14 express托管静态资源
2022-06-30 21:26:00 【画不完的饼】
express托管静态资源
express提供了一个非常好用的函数,叫做express.static(),通过它,我们可以非常方便地创建一个静态资源服务器,例如,通过如下代码就可以将public目录下的图片、CSS文件、JavaScript文件对外开放访问了。
//导入express
const express = require('express')
//创建web服务器
const app = express()
app.user(express.static('public'))
注意:Express在指定的静态目录中查找文件,并对外提供资源的访问路径。因此,存放静态文件的目录名不会出现在URL中。
//导入express
const express = require('express')
//创建web服务器
const app = express()
//在这里调用express.static()方法, 快速对外提供静态资源
//如果要对外提供多个静态资源目录,重复写更换文件目录即可
app.use(express.static('./clock'))
app.listen(80,(req,res)=>{
console.log('express server running at 127.0.0.1')
})
挂载路径前缀
如果希望再托管的静态资源访问路径之前,挂载路径前缀,则可以使用如下的方式:
//导入express
const express = require('express')
//创建web服务器
const app = express()
//在这里调用express.static()方法, 快速对外提供静态资源
//如果要对外提供多个静态资源目录,重复写更换文件目录即可
app.use('/public', express.static('./public'))
app.listen(80,(req,res)=>{
console.log('express server running at 127.0.0.1')
})
边栏推荐
- 文本生成模型退化怎么办?SimCTG 告诉你答案
- 等级测评是什么意思?工作流程包含哪些?
- Markdown笔记简明教程
- Upgrade Kube with unknown flag: --network plugin
- [grade evaluator] how to register a grade evaluator? How many passes?
- Radar data processing technology
- 物联网僵尸网络Gafgyt家族与物联网设备后门漏洞利用
- 凤凰架构——架构师的视角
- Multi table operation - foreign key constraint
- Metauniverse may become a new direction of Internet development
猜你喜欢
随机推荐
Clickhouse native monitoring item, system table description
k个一组反转链表
漫谈Clickhouse Join
Two skylines
[grade evaluator] how to register a grade evaluator? How many passes?
“信任机器”为发展赋能
Why have the intelligent investment advisory products collectively taken off the shelves of banks become "chicken ribs"?
Coefficient of variation method matlab code [easy to understand]
How to run jenkins build, in multiple servers with ssh-key
What does grade evaluation mean? What is included in the workflow?
ssh 默认端口不是22时的一些问题
Random talk about Clickhouse join
全面认识痛风:症状、风险因素、发病机理及管理
Flutter 嵌套地狱?不存在的,ConstraintLayout 来解救!
布隆过滤器
A group of K inverted linked lists
Understand what MySQL index push down (ICP) is in one article
jupyter notebook/lab 切换conda环境
Double solid histogram / double y-axis
Who are you and I









