简体   繁体   中英

posting an html with images, css, and javascript in node.js Express 4

I can use

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

to post this html. How do I display the html if it has images, css and javascripts that are included in the page. My view engine is jade.

You can use express.static middleware for serving static HTML / CSS / JS,

EXAMPLE:

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);

This will serve any static file under www folder, including HTML, JS and CSS files, and the rest (non existing files) will be catched by * which shows the 404

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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