Serving static files with NodeJS with node-static
Kyle (Simpson aka getify) recommends the node-static module
1. Set up a static file server
var staticServer = require('node-static');
var fileServer = new staticServer.Server('./front/dist');
The
./front/dist
will become the root for the static files. If your css file is in /front/dist/stylesheets/application.css then you need to request the /stylesheets/application.css file.2. Put fileServer in your http request handler
if you have:
httpServer = require("http").createServer(handleHTTP);
then in
function handleHTTP(req, res) {
if(req.url.startsWith("/stylesheets/")) {
req.addListener("end", function () {
fileServer.serve(req, res);
});
req.resume();
} else if (){ // more routing logic here }
and in your html:
<link rel="stylesheet" href="/stylesheets/application.css">