当前位置:网站首页>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')
})
边栏推荐
- 【无标题】
- Random talk about Clickhouse join
- 对多态的理解
- 文本生成模型退化怎么办?SimCTG 告诉你答案
- Clickhouse Native Monitoring item, System table Description
- Metauniverse may become a new direction of Internet development
- Three techniques for reducing debugging time of embedded software
- 企业保护 API 安全迫在眉睫
- Understanding polymorphism
- asp.net core JWT传递
猜你喜欢

【无标题】

ICLR'22 Spotlight | 怎样度量神经网络权重中的信息量?

《ClickHouse原理解析与应用实践》读书笔记(2)

ICML2022 | 序列决策的效用理论

企业保护 API 安全迫在眉睫

开源实习经验分享:openEuler软件包加固测试

Why have the intelligent investment advisory products collectively taken off the shelves of banks become "chicken ribs"?

毕业五年,想当初若没有入行测试,我是否还会如这般焦虑

防范未授权访问攻击的十项安全措施

Radar data processing technology
随机推荐
Sqlserver gets the data of numbers, Chinese and characters in the string
ICML2022 | 序列决策的效用理论
Introduction of 3D Max fine model obj model into ArcGIS pro (II) key points supplement
本地浏览器打开远程服务器上的Jupyter Notebook/Lab以及常见问题&设置
加密与解密以及OpenSSL的应用
Prediction and regression of stacking integrated model
雷达数据处理技术
Testing media cache
The 16th Heilongjiang Provincial Collegiate Programming Contest
A small step in code change and a big leap in thinking
测试媒资缓存问题
WebRTC系列-网络传输之本地scoket端口
等级测评是什么意思?工作流程包含哪些?
做一个 Scrollbar 的思考
多态在代码中的体现
Internet of things botnet gafgyt family and backdoor vulnerability exploitation of Internet of things devices
Metauniverse may become a new direction of Internet development
vim 常用快捷键
A group of K inverted linked lists
Learning summary