Basic static file server in NodeJS

后端 未结 8 777
盖世英雄少女心
盖世英雄少女心 2020-11-28 20:06

I\'m trying to create a static file server in nodejs more as an exercise to understand node than as a perfect server. I\'m well aware of projects like Connect and node-stati

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 20:22

    • Your basic server looks good, except:

      There is a return statement missing.

      res.write('404 Not Found\n');
      res.end();
      return; // <- Don't forget to return here !!
      

      And:

      res.writeHead(200, mimeType);

      should be:

      res.writeHead(200, {'Content-Type':mimeType});

    • Yes pipe() does basically that, it also pauses/resumes the source stream (in case the receiver is slower). Here is the source code of the pipe() function: https://github.com/joyent/node/blob/master/lib/stream.js

提交回复
热议问题