【问题标题】:Resource was blocked due to MIME type mismatch using pug and node由于使用 pug 和节点的 MIME 类型不匹配,资源被阻止
【发布时间】:2020-06-20 07:11:26
【问题描述】:

我正在尝试在 url 中呈现一个带有 id 的视图:

router.get('/employee', authController.protect, viewsController.getOverviewEmployee);
router.get('/employee/:id', authController.protect, viewsController.getOneEmployee);

/employee 工作正常,但是当我进入 /employee/:id 页面时,css 和脚本不会加载,控制台会显示此错误:

资源来自 “http://127.0.0.1:3000/employee/lib/bootstrap/css/bootstrapmin.css” 由于 MIME 类型(“应用程序/json”)不匹配而被阻止 (X-Content-Type-Options: nosniff)。

这是我的 index.pug 标头:

doctype html
head
  meta(charset='utf-8')
  meta(name='viewport' content='width=device-width, initial-scale=1.0')
  meta(name='description' content='')
  meta(name='author' content='Dashboard')
  meta(name='keyword' content='Dashboard, Bootstrap, Admin, Template, Theme, Responsive, Fluid, Retina')
  title Admin
  // Favicons
  link(href='img/favicon.png' rel='icon')
  link(href='img/apple-touch-icon.png' rel='apple-touch-icon')
  // Bootstrap core CSS
  link(href='lib/bootstrap/css/bootstrap.min.css' rel='stylesheet')
  link(rel='stylesheet', type='text/css', href='lib/bootstrap-fileupload/bootstrap-fileupload.css')
  // external css
  link(href='lib/font-awesome/css/font-awesome.css' rel='stylesheet')
  // Custom styles for this template
  link(href='dashcss/style.css' rel='stylesheet')
  link(href='dashcss/style-responsive.css' rel='stylesheet')

getOneEmployee:

exports.getOneEmployee = catchAsync(async (req, res, next) => {
    const employee = await Employees.findById(req.params.id);

    if (!employee) {
      return next(new AppError('No document found with that ID', 404));
    }

    res.status(200).render('admin/employeeManager',{
      title: 'Employee',
      employee
    });
});

和employeeManager.pug

extends index

block content
    section#container
        MAIN CONTENT
        // main content start
        section#main-content
            section.wrapper
                h3
                    i.fa.fa-angle-right
                    |  Editar Colaborador
                    .row.mt
                        .col-lg-12
                            h4
                                .form-panel
                                    .form
                                        form.cmxform.form-horizontal.style-form#commentForm(method='get' action)
                                            .form-group
                                                label.control-label.col-lg-2(for='cname') Nome*
                                                .col-lg-10
                                                    input.form-control#cname(name='name' minlength='2' type='text' required)
                                            .form-group
                                                label.control-label.col-lg-2(for='cphone') Telefone*
                                                .col-lg-10
                                                    input.form-control#cemail(type='cphone' name='phone' required)
                                            .form-group
                                                label.control-label.col-lg-2(for='cdescription') Descrição*
                                                .col-lg-10
                                                    input.form-control#curl(type='description' name='description' required)
                                            .form-group
                                                label.control-label.col-lg-2(for='ccomment') Your Comment (required)
                                                .col-lg-10
                                                    textarea.form-control#ccomment(name='comment' required)
                                            .form-group
                                                label.control-label.col-md-3 Image Upload
                                                .col-md-9
                                                    .fileupload.fileupload-new(data-provides='fileupload')
                                                        .fileupload-new.thumbnail(style='width: 200px; height: 150px;')
                                                            img(src='http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image', alt='')
                                                        .fileupload-preview.fileupload-exists.thumbnail(style='max-width: 200px; max-height: 150px; line-height: 20px;')
                                                        div
                                                            span.btn.btn-theme02.btn-file
                                                                span.fileupload-new
                                                                    i.fa.fa-paperclip
                                                                    |  Select image
                                                                span.fileupload-exists
                                                                    i.fa.fa-undo
                                                                    |  Change
                                                                input.default(type='file')
                                                            a.btn.btn-theme04.fileupload-exists(href='', data-dismiss='fileupload')
                                                                i.fa.fa-trash-o
                                                                |  Remove
                                                    span.label.label-info NOTE!
                                                    span
                                                        | Attached image thumbnail is
                                                        | supported in Latest Firefox, Chrome, Opera,
                                                        | Safari and Internet Explorer 10 only
                                            .form-group
                                                .col-lg-offset-2.col-lg-10
                                                    button.btn.btn-theme(type='submit') Salvar
                                                    | 
                                                    button.btn.btn-theme04(type='button') Cancelar

【问题讨论】:

    标签: node.js express pug mime-types


    【解决方案1】:

    这是因为您在属性中使用了相对 url。

    替换这个:

    link(href='img/favicon.png' rel='icon')
    

    有了这个:

    link(href='/img/favicon.png' rel='icon')
    

    为了进一步解释这一点,当您查看/employee 时,img/123.jpg 的相对链接被正确解析为/img/123.jpg。但是,只要您转到 url /employee/joeblow,那么相关链接就会寻找 /employee/img/123.jpg。您可以通过在浏览器中打开开发者工具然后查看“网络”选项卡中发出的请求来确认这一点。

    在所有img 元素的前面添加斜线将确保它们在正确的文件夹中查找图像,即根目录以外的文件夹 (/)。

    引导程序 mime 类型错误必须来自正确加载但由于上述问题而未找到依赖项的库之一。

    【讨论】:

    • 谢谢,我是 node.js 的新手。它解决了这个问题,感谢您的关注。
    猜你喜欢
    • 1970-01-01
    • 2021-08-11
    • 2021-03-21
    • 2020-04-09
    • 2021-02-23
    • 2023-03-20
    • 2019-10-14
    • 2021-01-10
    • 1970-01-01
    相关资源
    最近更新 更多