Session 11: Node.Js-I
1. Clone the following repository Repo.
2. Follow the instructions in the readme file to setup and run the cloned project.
3. Within the root directory of your project, you should see a folder with the name “views”. Within the views folder, there are four simple HTML files, namely: index.html, posts.html, contactus.html, and 404.html. As shown in the following figure
4. Run the server, you can do that by
> node server Or > nodemon server
Note: remember that if you used nodemon, you do not need to run your App again. If you are using npm, you need to stop your server if it is running (Ctrl + c), then, run it again every time you modify your application/project:
5. Navigate to the following pages by typing their addresses directly in your browser http://localhost:3000/, http://localhost:3000/posts, and to a page that does not exists like http://localhost:3000/cghbf so you are directed to the 404 page.
Note: do not write in the URL address bar the extension of the page. For example, writing (http://localhost:3000/posts.html) will direct you to the 404 page.
6. Inspect the code in the server.js, it is annotated, i.e., everything is described. Still, spend some time trying to understand it, and ask your teacher if there is anything not clear.
7. Create an app.get()
method to respond to Get requests to the contactus
page. After creating it, try to navigate to it http://localhost:3000/contactus
8. Add the following Middleware right after the app.listen(3000)
; Then, try to navigate to any of the pages that your server serves. Why you are not able to navigate to any page?
app.use((req, res) => { console.log('a new request was made to the server'); });
9. Modify the previous Middleware so that the server will work as expected.
app.use((req, res, next) => { console.log('a new request was made to the server'); next(); });