Skip to content

Forms and Data Handling: Fix code snippet #30004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mhndckngyn
Copy link

Because

The added lines of code fix an error that occurs when making a request to the app after running it: Error: Failed to lookup view "index" in views directory ...

This PR

  • Adds the line app.set("views", path.join(__dirname, "views"));, which set views to the directory where the template files are located.
  • Adds the line const path = require("node:path");, which imports the necessary module for the aforementioned line.

Issue

Closes #XXXXX

Additional Information

Pull Request Requirements

  • I have thoroughly read and understand The Odin Project curriculum contributing guide
  • The title of this PR follows the location of change: brief description of change format, e.g. Intro to HTML and CSS lesson: Fix link text
  • The Because section summarizes the reason for this PR
  • The This PR section has a bullet point list describing the changes in this PR
  • If this PR addresses an open issue, it is linked in the Issue section
  • If any lesson files are included in this PR, they have been previewed with the Markdown preview tool to ensure it is formatted correctly
  • If any lesson files are included in this PR, they follow the Layout Style Guide

@github-actions github-actions bot added the Content: NodeJS Involves the NodeJS course label Aug 16, 2025
@wise-king-sullyman wise-king-sullyman requested review from a team and wise-king-sullyman and removed request for a team August 16, 2025 16:19
@mhndckngyn
Copy link
Author

It looks like the lines I added aren't needed if the views directory is in the same location where the app is run. For example, running node ./app.js and making a request won't give an error if views are placed in ./views. It is necessary when the views directory is nested further though, like ./src/views.

This is because the Express app's views setting has the value process.cwd() + '/views' by default: https://expressjs.com/en/api.html#app.settings.table:~:text=process%2Ecwd%28%29%20%2B%20%27%2Fviews

So the error I encountered above only occurs if the views are placed in a subdirectory (./src/views in my case). The lesson does not specifically tell learners to put their code in a subdirectory, so the changes I made might not actually be necessary. It could make the setup more flexible however.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content: NodeJS Involves the NodeJS course
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant