当前位置:网站首页>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')
})
边栏推荐
- Is it safe to open an account for stock trading on mobile phones?
- 多表操作-外键约束
- To the Sultanate of Anderson
- 激发新动能 多地发力数字经济
- MySQL batch update
- Markdown笔记简明教程
- 本地浏览器打开远程服务器上的Jupyter Notebook/Lab以及常见问题&设置
- Radar data processing technology
- The 16th Heilongjiang Provincial Collegiate Programming Contest
- Why have the intelligent investment advisory products collectively taken off the shelves of banks become "chicken ribs"?
猜你喜欢
随机推荐
利用日志服务器输出各种apache的日志的TOPN
What happens when word encounters an error while trying to open a file?
ssh 默认端口不是22时的一些问题
Digital currency: far-reaching innovation
文本识别-SVTR论文解读
興奮神經遞質——穀氨酸與大腦健康
Radar data processing technology
Reading notes of Clickhouse principle analysis and Application Practice (1)
jupyterbook 清空控制台输出
遇到“word在试图打开文件时遇到错误”怎么办?
Auto-created primary key used when not defining a primary key
对多态的理解
Side sleep ha ha ha
Reading notes of Clickhouse principle analysis and Application Practice (2)
Analysis and proposal on the "sour Fox" vulnerability attack weapon platform of the US National Security Agency
Apply for vector bus protocol color picture wallpaper hanging picture, very good!
MySQL advanced 3
The 16th Heilongjiang Provincial Collegiate Programming Contest
CA I ah, how many times Oh, ah sentence IU home Oh 11111
Is it safe to open an account for stock trading on mobile phones?







