【问题标题】:posting an html with images, css, and javascript in node.js Express 4在 node.js Express 4 中发布带有图像、css 和 javascript 的 html
【发布时间】:2015-11-29 23:40:54
【问题描述】:

我可以使用

res.sendFile(BASEDIR + '/public/html/info.html');

发布此 html。如果页面中包含图像、css 和 javascript,我如何显示 html。 我的视图引擎是翡翠。

【问题讨论】:

    标签: html css express pug


    【解决方案1】:

    您可以使用express.static 中间件来提供静态 HTML / CSS / JS,

    示例:

    var express = require('express');
    var path = require('path');
    
    var server = express();
    
    server.use(express.static(__dirname + '/www'));
    server.use(express.static(path.join(__dirname, 'www')));
    server.get('*', function (request, response) {
      response.sendFile(__dirname + '/www/404.html');
    });
    
    server.listen(80);
    

    这将提供www 文件夹下的任何静态文件,包括 HTML、JS 和 CSS 文件,其余(非现有文件)将被 * 捕获,显示 404

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 2014-06-15
      • 2012-08-28
      • 2013-07-25
      • 2019-05-25
      • 1970-01-01
      相关资源
      最近更新 更多