how can node js file be serve code example

Example 1: how to use static file node js

app.use(express.static('public'))
app.use(express.static('files'))

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

Tags:

Html Example