I have already seen and made use of:
app.use("/css", express.static(__dirname + '/css'));
I do not wish to serve all files from the root directory, only a single file, 'ipad.htm'. What is the best way to do this with the minimum amount of code, using express.js?
res.sendfile(path_to_file);
is all you need; it will automatically set the correct headers and transfer the file (it internally uses the same code as express.static
).
fs.createReadStream(path).pipe(res);
I published a small library for serving single static files: https://www.npmjs.com/package/connect-static-file
npm install connect-static-file
var staticFile = require('connect-static-file');
app.use('/ipad', staticFile(__dirname + '/ipad.html'));
The main difference with express.static is that it does not care about the request url, it always serves that file if the route matches. It does not suddenly start serving an entire directory if you name your directory "ipad.html".
app.use("/css/myfile.css", express.static(__dirname + '/css/myfile.css'));