javascript - 使用 Pug 无法在 Express Js 中加载图像
问题描述
我已经仔细检查了路径并一次又一次地检查,但没有在浏览器上加载图像。我正在使用 Pug 引擎模板来为页面提供服务,除了图像没有加载之外,一切看起来都还不错。这是我在 VSC 中的文件结构和代码本身。
我在 chrome 浏览器上访问的本地主机上得到的输出
这是我的 app.js 代码
const express = require("express");
const path = require("path");
// const fs = require("fs");
const app = express();
const port = 80;
const express = require("express");
const path = require("path");
const app = express();
const port = 80;
// EXPRESS SPECIFIC STUFF
app.use('/static', express.static('staitc')) // For serving static files
app.use(express.urlencoded())
// PUG SPECIFIC STUFF
app.set('view engine', 'pug') // Set the template engine as pug
app.set('views', path.join(__dirname, 'views')) // Set the views directory
// ENDPOINTS
app.get('/', (req, res)=>{
// const con = "This is the best content on the internet so far so use it wisely"
// const params = {'title': 'PUBG is the best game', "constent": con}
const params = { }
res.status(200).render('home.pug', params);
})
app.get('/contact', (req, res)=>{
// const con = "This is the best content on the internet so far so use it wisely"
// const params = {'title': 'PUBG is the best game', "constent": con}
const params = { }
res.status(200).render('contact.pug', params);
})
// START THE SERVER
app.listen(port, ()=>{
console.log(`The application started successfully on port ${port}`);
});
解决方案
我忽略了声明的 2-times express
, path
, app
,port
在你的app.js
.
介绍一下我的方法。
app.js
// add under const values
app.use(express.static(path.join(__dirname, 'static')));
home.pug
img(src="/img/1.png", alt="Dance...")
推荐阅读
- ios - UITableView 高度在运行时不会通过 SnapKit 的按钮操作而改变
- python - 为什么当 2*2 的输出矩阵初始化为零并分配输出时,用于 2*2 矩阵加法的 python 程序为两行提供相同的输出?
- r - 标记用 grid.arrange 排列的图
- typescript - Is it possible to mimic webpack custom module resolution in TypeScript?
- ios - 无法将“Swift.__SwiftDeferredNSArray”(0x104cd8ae8)类型的值转换为“NSMutableArray”(0x7fff87c52960)
- javascript - 为什么 axios 在解析推特搜索 api 响应后会以某种方式修改推文的 id?
- asp.net - 我可以像 Tinymce 一样将 Gutenberg 文本编辑器与 Asp.net 明确集成吗?
- javascript - 在图片网址中添加字符串
- pdf - 谁使用 PDF 中的 `UserUnit` 属性?
- lua - 如何使用停止按钮停止音乐播放?