简体   繁体   English

无法通过 Express.js 中设置的静态文件目录在 Pug 中加载静态文件

[英]Not able to load static files in Pug althrough the static file directory has been set in Express.js

I am building an application and I am trying to send an email that uses Pug as a template engine and I am having problems loading static files in my template.我正在构建一个应用程序,我正在尝试发送一封使用 Pug 作为模板引擎的电子邮件,但在我的模板中加载静态文件时遇到问题。 My code successfully sets the public folder of my project as the static one and I am able to access the content of the files in the browser.我的代码成功地将我的项目的公用文件夹设置为静态文件夹,并且我能够访问浏览器中文件的内容。

However, the files are not loaded in my Pug template when I use a relative path with respect to the public directory.但是,当我使用相对于公共目录的相对路径时,文件不会加载到我的 Pug 模板中。 I also tried specifying their absolute path which actually works.我还尝试指定实际有效的绝对路径。

The directory structure of my application is:我的应用程序的目录结构是:

app

+/public

  +/css

    style.css

  image.png

+/src

  +/client

  +/server

    +/templates

      +/verify

        html.pug

        text.pug

    +server.js

server.js服务器.js

require('dotenv').config({path: __dirname + "/../../.env"});
const path = require('path');

const express = require('express');
const app = express();
const port = 8080;

const cors = require('cors');
const bodyParser = require('body-parser');

app.use(cors());
app.use(bodyParser.urlencoded({extended: true }));
app.use(bodyParser.json());

app.set('view engine', 'pug');
app.use(express.static(path.join(__dirname, '../../', 'public')));

const nodemailer = require('nodemailer');
const Email = require('email-templates');

const transporter = nodemailer.createTransport({
  address: 'smtp.gmail.com',
  host: 'smtp.gmail.com',
  port: 465,
  secure: true,
  service: 'gmail',
  auth: {
    user: process.env.SENDER_EMAIL,
    pass: process.env.SENDER_PASS
  },
  authentication: 'plain',
  enable_stattls_auto: true
});

const email = new Email({
  transport: transporter,
  views: {
    root: './templates'
  },
  send: true,
  preview: false
});

email.send({
  template: 'verify',
  message:  {
    from: process.env.SENDER_EMAIL,
    to: '*email*',
    subject: 'Activise - Email Verification',
  },
  locals: {}
})
.then(() => console.log('EMAIL SENT'))
.catch(err => console.log("ERROR: " + err));

app.listen(port, () => console.log('Server listening for requests on port 8080!'))

Here is my Pug code as well这也是我的 Pug 代码

html.pug html.pug

doctype html
html
    head
        title=title
        link(rel="stylesheet", href="/css/style.css" type="text/css")
    body
        .email-text
            img(src="/image.png", alt="Application logo")

in the server.js在 server.js 中

change改变

app.use(express.static(path.join(__dirname, '../../', 'public')));

to

app.use(express.static(path.join(__dirname, 'public')));

also change you pug to this也把你的哈巴狗改成这个

link(rel='stylesheet', href='/css/style.css')

Make sure that the path of the Public folder you have mentioned on the express.static middleware is correct.确保您在express.static中间件上提到的Public文件夹的路径是正确的。 It is currently pointed to two directories back from the current server.js file.它当前指向从当前 server.js 文件返回的两个目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM