Creating a server with Node.
Here’s the video I’m using to make a basic server.
The first thing I did was install Node.js. The video was made in 2016 so the installation wizard is a little different. It asked me if I wanted to automatically download tools I need to compile certain things I need using C/C++ and Python. I guess so. Aha, when I finished the installation I got a screen asking about using Chocolatey as a package manager, or something like that. Now it’s saying I have Python v3.11.0 installed, and it’s installed v3.12.4. Probably fine.
The video talks about the Node.exe file and I learned what a REPL is (read, eval, print, loop) – basically a command line specific to a program. I think I’ve seen this with MikTeX, Python, and maybe some other programs I’ve used. In this case you can just access all the Node commands through the ordinary Windows command line by typing node
. It doesn’t matter what directory you’re in to run Node. To exit, press CTL+C
twice. To run a .js file just type node filename
in cmd. You don’t need the file’s .js extension in the command but you need to be in the file’s directory. The video had me create a little file called app.js, but I don’t know what it’s for yet, if it’s for anything. Next is to create a package.json file, it looks like every application needs one, and I think it documents all the dependencies your application uses. You can write the .json file manually, or in cmd, you can type npm init
(npm is the Node package/module manager). Aha, the app.js file is the default entry point for my application. I think an entry point is the main file, similar to the index.html file of a website. Node asks if all your inputs for the init file are OK and if you type “no” then you have to start over. I forgot to add my name to “Author” so I had to start over.
Now to create the server using the app.js file. The video says this is the simple server that’s on the Node.js website, and sure enough, on the homepage there’s code to create a server. The loopback address is 127.0.0.1, and it’s used as the constant hostname
. I Googled “loopback address” and it looks like it’s more-or-less synonomous with “localhost”. I think that’s about all I understand about it for now. The server
constant uses the http module, which to me is confusing since I already named a constant http
. The constant just requires the http module, but I don’t know where, if ever, it’s called.
Here are the initializations for the server. There is a function with inputs req
for request and res
for response. The statusCode
method for the response object is set to 200, which means everything is OK. The setHeader
method gives the format of the page, so I think the response is the page that shows up in the browser when you go to localhost:3000. Then the response object needs an end
method.
Next is the server object with the listen
method. I don’t know what kind of object server
is, but the video mentions documentation for the http module. Looking at the documentation and then at my app.js file, I think I’ve defined server
to be a server class in the http module. That is where the listen
method comes from. This opens up the server, so you can find it by typing “localhost:3000” in the browser.
The video then adds another required module called fs
that lets you read in an .html file. This is the page you see when you go to the server in the browser. After making the modifications to app.js I went to restart the server and got the error “fs.readfile is not a function”. Found the error – it should be fs.readFile
. As I was trying to figure out the error I looked at the server code on the Node.js website. It’s not like mine, but I think I see exactly what it’s doing! Finally, to render the html on the page I need to change the Content-type
to text/html
. Done!
Well, that’s my goal for today. I still have some time, I think maybe I should put comments in my app.js file and/or try to read about Node.js in my book. I’m going to try to post to this blog more often. I’d started to wonder if it was unsustainable after not posting for 6 weeks but now I’m seeing how much better I understand Node by explaining it in my own words. Ultimately what I’m hoping is that others will find my posts useful, too.
Note: I actually posted this 3 Jul, not 4 Jul, but Jekyll gets confused when you write two blog posts in the same day and I haven’t figured out how to get around it.