Finally, the very last adventure that we need to complete. It's called JSON ME. All this adventure is about, is outputting JSON objects back. Well, you can use send, but there is a better method from the Express.js framework, it's called json. It's a method on the response object. The syntax is response.json, and then we pass the callback. And then we would be giving the input in argv and argv. The file, we'll need to read that file. The path to the file will be provided to us. So no need to create the file. In fact, they might be different files just to trick you to not hard-code it. So let's go to the editor. I'm in my favorite editor, it's called Atom. And I'm opening a new file, or creating a new file 8-json.js. And as typical, we would start the file by importing all the necessary dependencies. So we obviously need Express.js, and we need to instantiate that app.
And then we're using something called fs. It's a core module. No need to install anything. fs stands for the file system. And then I will route /books. It's a get route. Inside of that route, we have ... we basically need to read the file. So first let's get the filename from process.argv, and then we use fs.readFile. That's asynchronous method, so this method would actually show you how to write asynchronous code with Express.js. So inside of that asynchronous callback, every time process the error. That's very important to process the error, and then if we have an error, we need to send the status 500. Then I'm using JSON.parse so obviously I need to try/catch, because JSON.parse crashes applications if it doesn't like even like a single character. It's very picky. And then at the end, on line 14, I'm outputting the JSON back, and of course I'm ending everything by booting up the server to release it on the port. The port number will be provided and processed at argv. So this is very similar to, like, reading from a database. You would also have an asynchronous method, and then you would output your response inside of that callback. Why? Because if we do it here, here the books would be empty, because the books, line 16 happens before line 14. So we need to wait until the callback, the file is ready, or the database is ready with the data. In this case the books is not null. We parsed it, and only then output the response. So let's save it.
Let's go to the terminal. Let's start our server. Oh, before starting I didn't provide the filename so for this reason we will use expressworks because it will provide the filename for you. It will tell you if everything is correct, and this is correct. Now hit the Enter. Okay. Hooray! We've finished all the challenges! Ta-dum! But let me show you how to read a file, in case you're wondering. So again, I'm using this double pipe, or I can just delete that, but with the double pipe the code is smart enough to work for both the Express tool and for the manual entry. And then I want to use this file test.txt, which we already created. So what I will do, I will point to public/test.txt. And of course, this is not the best way. The better way would be to use path and __dirname, but it should work for now. So all I want to do is just try it really, really fast and show it to you.
One important thing I need to modify here in test.txt, it must be a valid JSON, otherwise our parser will just bork. So this should make it a valid JSON. Then we parse it, and then we should get the books. So now let's go to the terminal and restart it. Go to the browser. Okay, now we get the object. So that sums up our workshop. We have everything completed, and if you need a book, or if you want more materials, or if you spotted an error, you can go to this URL. Submit a pull request if you want to improve it, translate it. Also take a look at other amazing workshops at nodeschool. They also have free events in your cities. And that's it. Thank you.