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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.