Now I'm starting with a slightly different version of our files. I've added a folder here called data, and inside that data folder, I have a data.json file, which I'm going to import. This is just a JSON file that has an array of speakers, and some information about three different speakers. These are people that on our website are going to be in our different meetups. So if you're in your terminal, make sure that you have switched to the proper branch, the branch for this project is a 106B, so you would do a git checkout of 01_06b, and you would get to this stage of the process.
Remember that if you have some unsafe files, or some files that are different from the ones that I have, you can do a git stash command, and that will ignore any other files or stash them, so that you can get back to them later. I'm already on this branch, so I'm gonna start by coming over here and loading up our data file. So I'll create a variable called dataFile, and that's gonna be a required statement. Just like we did with our different libraries, we can require files and store them in a variable, and that works really well because that particular file is already a JSON file, so it will automatically import into this variable very well.
So I'll say I wanna go to the data folder and the data.json file. This isn't a library or something that we've installed with NodeJS, so in this case we need to use a path that is relative to our current file. Now in my server, I am hard coding a port number and that's not always a good idea. If I want to, I can modify that so that it reads an environment variable from NodeJS automatically, and we can modify how our application loads with different ports.
A lot of times we're running different applications already on different ports, and so we want this application to be flexible, and we can do that by using another method called app.set That's going to set an environment variable for our application, so I can say, "I wanna set the port number "of this application "to be whatever my environment variable "for Node is, called PORT." And what I'll do also is, just in case somebody doesn't provide a port, I'm going to default to port 3000.
Now that means that this variable right here will be a little bit different. So I can still listen on a port, but in here, I can use that same app.get method to get that port variable. And then do the same thing here, web server listening on port 3000, and just use that same plus app.get port, and now when I run the application, I can specify the port. If I don't, then it's going to assume that I want to use port 3000.
So let's check that out. Go ahead and save this, and we'll run the application again. It already looks like it's running in our browser. Let's go ahead and refresh that, and you can see that it works fine just like that. I don't necessarily have to specify a port, but if I want to, now I can. So let's go ahead and do the same thing, node app/app.js, but before that, I'm going to set the port to be something else. Now notice that the message set listening on port 4000, if I try to reload right here it will give me an error until I change the port number.
So now I can do something like this, and I can use the regular list item tag here and multi-line text templates. So I'm gonna put in an h2, and I'm going to use the dollar sign and curly braces to pull in my variable called name from this temporary item variable that loops through each of the speakers, and then I'm going to add a paragraph and use that same template string variable to get the summary for each speaker.
I'll go ahead and add another paragraph here and close out my list item right here. And now in my send command, instead of using the straight quotes, I can also use the back-ticks. So let's do that, that way I can send a multi-line string in here, as well, and I'll put my headline and then I'll pass along my info variable right here. Let's go ahead and save this and go back and run our application.
In this course, Ray Villalobos walks through the process of creating full-featured websites with these technologies. First, find out how to install each package and leverage Node.js features from within Express. Then learn how to build a basic application with templating engines like EJS; create more flexible, modular code with includes and conditional statements; build APIs to manage HTTP requests to add and delete content dynamically; and configure more complex routing. Along the way, Ray implements features like customer feedback forms and real-time live chat, so you can see exactly what Node.js and Express are capable of.
- Handling HTTP requests with Node.js and Express
- Creating and modularizing routes
- Using templates
- Building flexible views
- Creating an API route
- Dealing with POST and DELETE requests
- Creating live chat features with Socket.IO