how to serve a file with nodejs code example
Example 1: access to static file nodejs
app.use(express.static(__dirname + '/public'));
<link rel="stylesheet" type="text/css" href="css/style.css" />
Example 2: how to use static file node js
app.use('/static', express.static(path.join(__dirname, 'public')))
Example 3: how can node js file be serve
var fs = require('fs'),
http = require('http');
http.createServer(function (req, res) {
fs.readFile(__dirname + req.url, function (err,data) {
if (err) {
res.writeHead(404);
res.end(JSON.stringify(err));
return;
}
res.writeHead(200);
res.end(data);
});
}).listen(8080);
Example 4: how to use static file node js
app.use('/static', express.static('public'))