I am writing HTTP server on top of Node.js, without using HTTP module.
Retrieving a GET message: GET /www/index.html HTTP/1.1 should send a response with the file located at /www/index.html, same with images and other files.
Could anyone guide me to a javascript way to fetch the files from disk and add them to my HTTP response ?
My server should be able to support the following content types :
- JavaScript: application/javascript
- HTML: text/html
- CSS: text/css
- JPEG: image/jpeg
- GIF: image/gif
I think the
fsmodule is what you are looking for.It provides various function to open and read from files. A simple example :
You can also consult this blog post about reading files with Node : Different ways to read in files using node.js