serve.js 849 B

1234567891011121314151617181920212223242526272829
  1. /*
  2. Script for serving index.html and other static content with Node.
  3. Run it using `node serve` from your terminal and navigate to http://localhost:5000
  4. in order to test your changes in the browser.
  5. */
  6. var http = require('http'), fs = require('fs'), mimeTypes = {
  7. 'html': 'text/html',
  8. 'css': 'text/css',
  9. 'js': 'text/javascript',
  10. 'json': 'application/json',
  11. 'png': 'image/png',
  12. 'jpg': 'image/jpg'
  13. };
  14. http.createServer(function (req, res) {
  15. var file = (req.url === '/') ? 'index.html' : "." + req.url;
  16. var ext = require('path').extname(file),
  17. type = (mimeTypes[ext] ? mimeTypes[ext] : '');
  18. fs.exists(file, function (exists) {
  19. if (exists) {
  20. res.writeHead(200, {'Content-Type': type});
  21. fs.createReadStream(file).pipe(res);
  22. } else {
  23. console.warn(file, ' does not exit');
  24. }
  25. });
  26. }).listen(5000);