Previous Lecture Complete and continue  


Welcome to Express Works. This is the second adventure, so let's go ahead and bring up the menu. It shows the first adventure is completed. If you haven't completed it, you can go back and watch that video. Now, let's select the second adventure which is called Static. It shows us the problem and some hints. As always, we will try to solve the problem without looking at the hints. So, what it wants us to do, it wants us to apply the static middleware to serve some files. For example, index.html, and the path to that file, where it's basically the folder where that file is located will be given to us in this variable process.argv[3], and the port, as always, will be faster to process.argv[2]. So, for thiswe don't need any routes, just the static middleware, and this is the file so you can copy and paste. Let's go to create a similar file, but just be aware of the white spaces. I will just use the file provided by the workshop itself. Okay, so, let's go back to the editor, and create a new file. I will be using the naming convention with the number of the adventure followed by the name so, it's 2-static. As always, we start by porting the required path it says. The path is core module so we don't need to install it.

Express is npm module so we've already installed it in the first exercise, Hello World. Then on the line three we instantiate that express app, and on line five we use app.use method, and then we apply static middleware. We're pathing the folder name and also in case that variable is no, we want to fold back on the public folder name, and then we'll listen to the port number provided in the second argument, which is actually first argument because argv is a zero-based rate, and the first item would be the command line node js, and the second item would be program name. So, we start the first argument with number two, and the second argument with number three. But you don't need to remember it unless you want to run this double manually, which I don't recommend, you need to create a semicolon. If this looks complex to you, basically what's happening here, we have path module and then we call joint. So, joint, it's a cross platform method to create folder paths, because in Windows and Lennox, the folder separation is different, it's backwards and forward slash. So, what we do, we just provide a bunch of strings into this method and then we'll get nicely formatted, normalized path. It's optional if you want to play and create a public folder right here.

Let's go ahead and do it. So, everything from this public folder will be served. So, I can create even a text file with this server, but that's not what the example is asking. So, if you want to you can create index.html in that folder, and then get rid of this argv, but I do not recommend it because you might introduce some white space. But I don't recommend it because you might introduce some white space. So, it should be working just fine when we run it. So, let's go ahead and try it. Then I'm using two. Okay, it works just fine. We have the output, which is provided by the workshop. So, this file is provided by the workshop. We didn't create it. Now, we can verify and obviously it's passing. So my solution looks exactly the same as the solution provided. One more thing before starting the server when you do have this port number. The fold back because we are not providing any arguments. So, now I'm restarting the server again, and if I type tesa.txt I see this file. So, that's how you can serve any content. Just be aware to make this public folder not to make it your root, not to expose any of your node.js files because you don't want to expose that code. You might have some more sensitive API keys or passwords to database file. So, let's go back to the terminal and bring up the menu, and it shows that it is completed.