Serving from Files

Being able to write and run JavaScript on your server is nice. Most servers will want to vend out and process content living in files, too. Your next job is to make your server read files from a subfolder and send them in a response back to the browser. This is similar to what browser-sync was doing for you in earlier chapters.

Create a new folder named app inside of your chattrbox project folder. In it, create an index.html file with the following text:

Hello, File!

You do not need any actual HTML in this file – it only needs some content that can be read. Your project folder should look like Figure 15.11.

Figure 15.11  Chattrbox project layout

Reading a file with the fs module

In index.js ...

Get Front-End Web Development: The Big Nerd Ranch Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.