histsrv2.js 632 B

1234567891011121314151617181920212223
  1. const http = require('http')
  2. const fs = require('fs')
  3. const httpPort = 3000
  4. http.createServer((req, res) => {
  5. // console.log(req);
  6. console.log('Serving file.','.'+req.url);
  7. var fn = fs.existsSync('.'+req.url) ? '.'+req.url : "demorouter2.html";
  8. console.log('Actually Serving file.',fn);
  9. fs.readFile(fn, 'utf-8', (err, content) => {
  10. if (err) {
  11. console.log('We cannot open "'+fn+'" file.')
  12. }
  13. res.writeHead(200, {
  14. 'Content-Type': 'text/html; charset=utf-8'
  15. })
  16. res.end(content)
  17. })
  18. }).listen(httpPort, () => {
  19. console.log('Server listening on: http://localhost:%s', httpPort)
  20. })