Skip to content

fix: update reference to Pug version for Set up a Template Engine challenge - #38572

Merged
ojeytonwilliams merged 7 commits into
freeCodeCamp:masterfrom
jeremyfiel:fix/advanced-node-and-express--set-up-a-template-engine.english
Apr 23, 2020
Merged

ojeytonwilliams merged 7 commits into
freeCodeCamp:masterfrom
jeremyfiel:fix/advanced-node-and-express--set-up-a-template-engine.english

Conversation

@jeremyfiel

@jeremyfiel jeremyfiel commented Apr 19, 2020 •

Copy link
Copy Markdown
Contributor

Relates to freeCodeCamp/boilerplate-advancednode#9.

Checklist:

  • I have read freeCodeCamp's contribution guidelines.
  • My pull request has a descriptive title (not a vague title like Update index.md)
  • My pull request targets the master branch of freeCodeCamp.
  • All the files I changed are in the same world language, for example: only English changes, or only Chinese changes, etc.

Closes #XXXXX

This is to update the Pug dependency version in the instructions for this challenge. This PR is a dependent of the PR referenced in the advanced boilerplate repo. I completely updated the boilerplate including the dependencies.

Thanks!

@jeremyfiel
jeremyfiel requested a review from a team April 19, 2020 03:44
@gitpod-io

gitpod-io Bot commented Apr 19, 2020 •

Copy link
Copy Markdown

@scissorsneedfoodtoo scissorsneedfoodtoo changed the title update reference to Pug version, relates to {freeCodeCamp/boilerplate… fix: update reference to Pug version for Create New Middleware challenge Apr 20, 2020
@scissorsneedfoodtoo scissorsneedfoodtoo changed the title fix: update reference to Pug version for Create New Middleware challenge fix: update reference to Pug version for Set up a Template Engine challenge Apr 20, 2020
@scissorsneedfoodtoo

Copy link
Copy Markdown
Contributor

This change looks good and partially addresses #38404.

I believe we'll need to modify a current test or add a new test to fully address the problem where res.sendFile(...) passes even though the instructions say to change that method to res.render(...).

The problem seems to be that the test suite doesn't recognize the difference between sendFile and render, and probably receives the same data either way. We'll need to add or modify a test so it's something like the second testString here that does a regex check on what's in server.js:

testString: getUserInput => $.get(getUserInput('url')+ '/_api/server.js') .then(data => { assert.match(data, /('|")view engine('|"),( |)('|")pug('|")/gi, 'Your project should set Pug as a view engine'); }, xhr => { throw new Error(xhr.statusText); })

We'll need to check that people use res.render(...) directly.

But that can be handled in another PR. Would you be interested in working on that? Everything here LGTM.

@scissorsneedfoodtoo
scissorsneedfoodtoo self-requested a review April 20, 2020 07:07

@ojeytonwilliams ojeytonwilliams left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the changes to package-lock.json?

LGTM, otherwise.

….english' of github.com:jeremyfiel/freeCodeCamp into fix/advanced-node-and-express--set-up-a-template-engine.english
@jeremyfiel

Copy link
Copy Markdown
Contributor Author

Could you remove the changes to package-lock.json?

LGTM, otherwise.

@ojeytonwilliams i tried updating and pushing another commit but it seems like it didn't take it. I'm thinking the build is modifying it some how. What do you think?

@ojeytonwilliams
ojeytonwilliams force-pushed the fix/advanced-node-and-express--set-up-a-template-engine.english branch from deeeab3 to 70cf3fb Compare April 20, 2020 15:45
@ojeytonwilliams

Copy link
Copy Markdown
Contributor

@jeremyfiel I'm not quite sure what was going wrong, but this is how I fixed it:

git rebase master
git checkout master package-lock.json
git commit --amend

Possibly it had been reintroduced during the merge. Not sure!

@jeremyfiel

Copy link
Copy Markdown
Contributor Author

This change looks good and partially addresses #38404.

I believe we'll need to modify a current test or add a new test to fully address the problem where res.sendFile(...) passes even though the instructions say to change that method to res.render(...).

The problem seems to be that the test suite doesn't recognize the difference between sendFile and render, and probably receives the same data either way. We'll need to add or modify a test so it's something like the second testString here that does a regex check on what's in server.js:

testString: getUserInput => $.get(getUserInput('url')+ '/_api/server.js') .then(data => { assert.match(data, /('|")view engine('|"),( |)('|")pug('|")/gi, 'Your project should set Pug as a view engine'); }, xhr => { throw new Error(xhr.statusText); })

We'll need to check that people use res.render(...) directly.

But that can be handled in another PR. Would you be interested in working on that? Everything here LGTM.

i'm not entirely sure we need to assert using render if the boilerplate is updated to use it.
The instructions state the following line but i've already updated the boilerplate to use render and the rest of the exercises never use sendFile anyways. This seems like we should update the boilerplate to set the method to render but maybe leave the path parameter blank and get the user to update it to "index.pug" to point to the correct files to render.

Another option is we can leave the block inside the get method blank and ask them to update the entire block.
either of these makes more sense the way the test is written and we can always update the tests to further assert the use of render if the code block is left blank.

Something I just noticed is the path is actually completely misleading because it's using .html instead of .pug. I can see how that would be confusing.

Lastly, you should change your response to the request for the index route to res.render with the path to the view views/pug/index.pug.
app.route('/')
.get((req, res) => {
res.sendFile(process.cwd() + '/views/index.html'); Should we remove this sample code?
});

My proposal is:

app.route('/')
.get((req, res) => {
// Write your code here to correctly render the index page
});

then modify the tests to assert render is used and the path is matching the expectation.

@ojeytonwilliams

Copy link
Copy Markdown
Contributor

My proposal is:

app.route('/')
.get((req, res) => {
// Write your code here to correctly render the index page
});

then modify the tests to assert render is used and the path is matching the expectation.

Sure, though I think it's important that the page does not hang. How about:

app.route('/')
.get((req, res) => {
   res.send('replace this!'); // Write your code here to correctly render the index pag
});

or similar. Just something to make sure that their app works straight away.

@jeremyfiel

Copy link
Copy Markdown
Contributor Author

My proposal is:

app.route('/')
.get((req, res) => {
// Write your code here to correctly render the index page
});

then modify the tests to assert render is used and the path is matching the expectation.

Sure, though I think it's important that the page does not hang. How about:

app.route('/')
.get((req, res) => {
   res.send('replace this!'); // Write your code here to correctly render the index pag
});

or similar. Just something to make sure that their app works straight away.

i tested it on the original glitch repo and it shows "Site didn't respond" which could confuse campers although it replies with 200, I doubt most are logging the response codes to understand that aspect. It's not finding the root page because the route is empty.
If we set it to res.sendFile with an invalid parameter they are actually getting an error instead. I think we can explain this a bit in the article. Something to the affect,

Your page will not load until you correctly render the index file. Using the "Show" menu at the top of the Glitch page will allow you to view your app next to your code. This is helpful to see if your page is loading as expected. If you are seeing the page with a message "Site didn't respond", this indicates your routing is incomplete on the root path. You can use a relative path, absolute path or native Node methods such as "process.cwd()" to point the route to the index file.

I think asking a camper to hunt for native node methods is a bit far fetched vs asking them to review the express docs for the render function, although this is supposed to be "Advanced Node". hopefully the hint of saying "render" will be enough and I can link to the Node methods.

Because this is the boilerplate for the beginning of Advanced Node, the app is currently not in a working state anyways. This setup work is the starting point.
I've read a lot of complaints over the past year where campers are very confused with the advanced node coursework. I hope this can be a new beginning to really set them up for success. It will require going through the other boiler plates to align them.

@ojeytonwilliams

Copy link
Copy Markdown
Contributor

I think asking a camper to hunt for native node methods is a bit far fetched vs asking them to review the express docs for the render function, although this is supposed to be "Advanced Node". hopefully the hint of saying "render" will be enough and I can link to the Node methods.

That's a very good point. I just tried this out and had to go to the express docs because render('views/pug/index.pug') does not work and the instructions imply that you should use views/pug/index.pug. My preference would be to guide people to the concise solution render('pug/index'), rather than using any node methods. Perhaps the instructions could be:

Lastly, you should change your response to the request for the index route to render the file in views/pug/index.pug. res.render() takes a relative path (relative to views) as its argument and does not require an extension.

We could link to the express docs, too. Generally we don't use external links, but when we're relying on a library I think it's fine.

@camperbot camperbot added language: English scope: curriculum Lessons, Challenges, Projects and other Curricular Content in curriculum directory. labels Apr 20, 2020
@jeremyfiel

Copy link
Copy Markdown
Contributor Author

I think asking a camper to hunt for native node methods is a bit far fetched vs asking them to review the express docs for the render function, although this is supposed to be "Advanced Node". hopefully the hint of saying "render" will be enough and I can link to the Node methods.

That's a very good point. I just tried this out and had to go to the express docs because render('views/pug/index.pug') does not work and the instructions imply that you should use views/pug/index.pug. My preference would be to guide people to the concise solution render('pug/index'), rather than using any node methods. Perhaps the instructions could be:

Lastly, you should change your response to the request for the index route to render the file in views/pug/index.pug. res.render() takes a relative path (relative to views) as its argument and does not require an extension.

so actually the relative path would only work if the server and index were in the same directory, sub directories don't count. that's not the case so the absolute path can be achieved a few different ways. The main point is we need to drill down into directories because we are starting at the base path /app. we can confirm this by logging process.cwd() // /app. Using the extension doesn't really matter although the express docs state you don't need it. So I guess the tests need to be updated to allow for either some of these, or all of these variations. This is a pretty heavy regex request. I'm not sure if there is a better way to test if the page rendered successfully vs actually parsing the server.js for the matching data. I found an example on stack using mocha/chai where we can just test if the page loaded and read some text on the page from the index.pug. This seems much simpler than expecting a bunch of text variations in server.js.

describe('Home Page', () => {
    it('This is the Home page', (done)=> {
        chai.request(server)
        .get('/home')
        .end((error, respond) => {            
            expect(/Welcome/, done);                     
        done();
        });
    }); 
})
//All of these work
/app/views/pug/index
/app/views/pug/index.pug
../views/pug/index
../views/pug/index.pug
./pug/index
./pug/index.pug
${process.cwd()}/views/pug/index
${process.cwd()}/views/pug/index.pug

We could link to the express docs, too. Generally we don't use external links, but when we're relying on a library I think it's fine.

Ok.

….english' of github.com:jeremyfiel/freeCodeCamp into fix/advanced-node-and-express--set-up-a-template-engine.english
@jeremyfiel

jeremyfiel commented Apr 21, 2020 •

Copy link
Copy Markdown
Contributor Author

I added a new test for {/issues/38404} and reworded the instructions a bit to make more sense and lead the camper to a successful result without giving them the answer.

@scissorsneedfoodtoo

Copy link
Copy Markdown
Contributor

@jeremyfiel and @ojeytonwilliams, awesome discussion. I'm learning a lot, so thank you for all the details you've included here.

@jeremyfiel, like you mentioned, it does make sense to change the boilerplate here to more effectively lead learners to the right solution, especially since it's the first challenge in this section. And I think you're right, we shouldn't have to rely on regex testing if we set things up so learners have to use .render() from the beginning. It'll make things much easier and cleaner from a testing perspective, and we can use a test like the one you posted above that checks for specific test on the home page.

Given all the possible ways to render the page, maybe the text could be something like:

"Lastly, you should change your response to the request for the index route to render the file in views/pug/index.pug. res.render() takes a string of a file path an argument. The path can be a relative path (relative to views), or an absolute path, and does not require an extension."

@jeremyfiel

Copy link
Copy Markdown
Contributor Author

@jeremyfiel and @ojeytonwilliams, awesome discussion. I'm learning a lot, so thank you for all the details you've included here.

@jeremyfiel, like you mentioned, it does make sense to change the boilerplate here to more effectively lead learners to the right solution, especially since it's the first challenge in this section. And I think you're right, we shouldn't have to rely on regex testing if we set things up so learners have to use .render() from the beginning. It'll make things much easier and cleaner from a testing perspective, and we can use a test like the one you posted above that checks for specific test on the home page.

Given all the possible ways to render the page, maybe the text could be something like:

"Lastly, you should change your response to the request for the index route to render the file in views/pug/index.pug. res.render() takes a string of a file path an argument. The path can be a relative path (relative to views), or an absolute path, and does not require an extension."

so I actually added the test using regex to verify the server.js has the expected input. There is already another test checking for data returned on the template after it loads. I went the regex direction because we are trying to coerce campers into specific methods being used in the actual code, rather than only checking the result. If we were in a production environment, this type of test wouldn't be necessary.
Thanks for the comments! Appreciate it!

….english' of github.com:jeremyfiel/freeCodeCamp into fix/advanced-node-and-express--set-up-a-template-engine.english

Revise api response when Pug is not rendered successfully
jeremyfiel added a commit to jeremyfiel/boilerplate-advancednode that referenced this pull request Apr 21, 2020
… server. fixes {freeCodeCamp/freeCodeCamp/issuse/38404}

Revise server.js to update instructions to match Challenge.md on main repo {freeCodeCamp/freeCodeCamp/pull/38572}
@jeremyfiel

Copy link
Copy Markdown
Contributor Author

@ojeytonwilliams i updated the instructions here too so they coincide with the boilerplate.

jeremyfiel added a commit to jeremyfiel/boilerplate-advancednode that referenced this pull request Apr 21, 2020
… server. fixes {freeCodeCamp/freeCodeCamp/issuse/38404}

Revise server.js to update instructions to match Challenge.md on main repo {freeCodeCamp/freeCodeCamp/pull/38572}

@ojeytonwilliams ojeytonwilliams left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nearly there! I think we need to go into the details of how to to render a file, to avoid people giving up at the first challenge.

jeremyfiel added a commit to jeremyfiel/boilerplate-advancednode that referenced this pull request Apr 21, 2020
… server. fixes {freeCodeCamp/freeCodeCamp/issuse/38404}

Revise server.js to update instructions to match Challenge.md on main repo {freeCodeCamp/freeCodeCamp/pull/38572}
scissorsneedfoodtoo pushed a commit to freeCodeCamp/boilerplate-advancednode that referenced this pull request Apr 23, 2020
* update depds, remove unnecessary local files, update readme, package, server.  fixes {freeCodeCamp/freeCodeCamp/issuse/38404}

* Revise server.js to update instructions to match Challenge.md on main repo {freeCodeCamp/freeCodeCamp/pull/38572}
@jeremyfiel
jeremyfiel force-pushed the fix/advanced-node-and-express--set-up-a-template-engine.english branch from 606bbe0 to 159152f Compare April 23, 2020 06:55
Revise new test to account for white space formatting
@jeremyfiel
jeremyfiel force-pushed the fix/advanced-node-and-express--set-up-a-template-engine.english branch from 159152f to 3d6ebcd Compare April 23, 2020 06:58
@ojeytonwilliams

Copy link
Copy Markdown
Contributor

@scissorsneedfoodtoo I'm merging this now, because @jeremyfiel pointed out that the instructions will be out of sync with the boilerplate. Let me know if there's anything else needing changing and I'll create the PR.

@ojeytonwilliams
ojeytonwilliams merged commit 1ee0334 into freeCodeCamp:master Apr 23, 2020
@ojeytonwilliams

Copy link
Copy Markdown
Contributor

@jeremyfiel nice work! Thanks again for taking this on.

@WihanBarnard

WihanBarnard commented Apr 28, 2020 •

Copy link
Copy Markdown

Hi all,

Thanks for the thorough discussion. I started working through the advanced node and express section today and I could not pass the third test on the first challenge ("Use the correct ExpressJS method to render the index page from the response."). I see that there is one other camper who posted on the forum approx 3 days ago, and I see these changes were made quite recently (https://www.freecodecamp.org/forum/t/set-up-a-template-engine-keeps-failing/378920). The piece of code in the server.js from the glitch project is as follows:

app.route("/").get((req, res) => {
  //Change the response to render the Pug template
  res.render(process.cwd() + '/views/pug/index');
});

@jeremyfiel

Copy link
Copy Markdown
Contributor Author

Hi all,

Thanks for the thorough discussion. I started working through the advanced node and express section today and I could not pass the third test on the first challenge ("Use the correct ExpressJS method to render the index page from the response."). I see that there is one other camper who posted on the forum approx 3 days ago, and I see these changes were made quite recently (https://www.freecodecamp.org/forum/t/set-up-a-template-engine-keeps-failing/378920). The piece of code in the server.js from the glitch project is as follows:

app.route("/").get((req, res) => {
  //Change the response to render the Pug template
  res.render(process.cwd() + '/views/pug/index');
});

you should be seeing this in the glitch project if you followed the link in the challenge "Setting up a template engine". If you are using any other boilerplate then it's not updated as of yet.

app.route("/").get((req, res) => {
  //Change the response to render the Pug template
  res.send(`Pug template is not defined.`);
});

This was part of our discussion above where we mentioned avoiding node process methods in favor of relative path structuring. While the node process is technically successful, we didn't include the ability to use it in the tests.

The path can be a relative path (relative to views), or an absolute path, and does not require a file extension.

@ojeytonwilliams

Copy link
Copy Markdown
Contributor

@jeremyfiel's pretty much covered it, but I can confirm that new projects created via the link in As a reminder, this project is built upon the following starter project on Glitch use the new boilerplate.

@WihanBarnard if you've just started that section, I'd recommend starting a few project with the new boilerplate.

@WihanBarnard

WihanBarnard commented Apr 28, 2020 •

Copy link
Copy Markdown

Thanks very much for the replies @jeremyfiel and @ojeytonwilliams ! I did continue the discussion on the forum, but though I could mention my finding here also:

When looking at the regex test for the third test:

assert.match(data, /res(\s+)?\.(r\w{5})\(('|")((\.{2})?)\/?(\w{3})\/(\w{5})('|")\)(;)?/gi, 'You successfully rendered the Pug template!');

UPDATE:
This also satisfies the regex

res.render('pug/index');

(this was my first try, BUT I had to change

app.use("/public", express.static(process.cwd() + "/public"));

TO


app.use("/public", express.static(process.cwd() + "/views"));

Is this intentional or is an update on the boilerplate needed?

@jeremyfiel

jeremyfiel commented Apr 28, 2020 •

Copy link
Copy Markdown
Contributor Author

Thanks very much for the replies @jeremyfiel and @ojeytonwilliams ! I did continue the discussion on the forum, but though I could mention my finding here also:

When looking at the regex test for the third test:

assert.match(data, /res(\s+)?\.(r\w{5})\(('|")((\.{2})?)\/?(\w{3})\/(\w{5})('|")\)(;)?/gi, 'You successfully rendered the Pug template!');

UPDATE:
This also satisfies the regex

res.render('pug/index');

(this was my first try, BUT I had to change

app.use("/public", express.static(process.cwd() + "/public"));

TO


app.use("/public", express.static(process.cwd() + "/views"));

Is this intentional or is an update on the boilerplate needed?

copying from the forum thread...

The way the Express static method is outlined in this boilerplate is only for the CSS assets. If you change the static path to views, it will never display the css, which I supposed is meaningless for most of this curriculum because it’s focused on a node app (backend).
But I digress, the expectation is as you indicated in your update but the changes to the static resource are unnecessary.
the regex is expecting (.{2})? there “might be” 2 leading periods but it’s tricky because you’re right, it only works with using ../views/… or not at all. But the directions imply "relative to views". Maybe we can take a look at modifying the test a bit.
Hope it helps!

@scissorsneedfoodtoo

Copy link
Copy Markdown
Contributor

Thanks for bringing this up @WihanBarnard. Like @jeremyfiel and @ojeytonwilliams have mentioned, the boilerplate was updated recently, along with the regex test you pointed out above.

Maybe we should loosen up that test so it really is just checking for res.render(); rather than trying to account for every possible path that can be passed as an argument. Perhaps something pretty permissive like res(\s+)?\.(r\w{5})\(('|")?.*('|")?\)\;?.

The last test for this challenge checks that the page is rendered properly whether by using Node processes or a relative path.

What do you all think?

@ojeytonwilliams

Copy link
Copy Markdown
Contributor

Yep, the last test makes sure the right thing is rendered, so the main thing is to check is 'is render used?'. How about a simplified version of @scissorsneedfoodtoo's:

res\s*\.render\(.*?\)

i.e. specify render and allow basically everything else

or

res\s*\.render\([^'"]*?('|").*(\1)\)

i.e. res.render(possibly 'node.cwd() + ' followed by something in quotes). This should match

res.render('pug/index');
res.render(process.cwd() + '/views/pug/index');

@jeremyfiel

Copy link
Copy Markdown
Contributor Author

i think we can go on forever with regex but let's relax the test to explicitly check only the method without revealing the name in the actual test. anything after the method begins is left unchecked.

res\s*\.r(\w{5})

@scissorsneedfoodtoo

Copy link
Copy Markdown
Contributor

@ojeytonwilliams thank you for simplifying that. Either one is much more readable than the one I posted above.

@jeremyfiel, that could work, and I'm always up for simplifying wherever possible. My only concern with res\s*\.r(\w{5}) is that something like res.reeeee('test'); would still pass that test.

I think it would be better to test for the specific method we tell them to use in the instructions. Campers won't be shown the answer on the challenge's page. If they don't use the render method, all they'll see is "Use the correct ExpressJS method to render the index page from the response."

@jeremyfiel

jeremyfiel commented May 1, 2020 •

Copy link
Copy Markdown
Contributor Author

@ojeytonwilliams thank you for simplifying that. Either one is much more readable than the one I posted above.

@jeremyfiel, that could work, and I'm always up for simplifying wherever possible. My only concern with res\s*\.r(\w{5}) is that something like res.reeeee('test'); would still pass that test.

I think it would be better to test for the specific method we tell them to use in the instructions. Campers won't be shown the answer on the challenge's page. If they don't use the render method, all they'll see is "Use the correct ExpressJS method to render the index page from the response."

well, i suppose they could but then the 4th test would fail and the page wouldn't load without using render because express doesn't have a reeeee method. So they still wouldn't be able to pass the challenge. I think the purpose of using the occurrence is to avoid them looking up the test answer explicitly.. even if they did, they still need to dig a bit more on the express docs to figure it out.. or by connecting the dots on the test message and the challenge instructions. if you prefer, i can change it.

@jeremyfiel
jeremyfiel deleted the fix/advanced-node-and-express--set-up-a-template-engine.english branch June 12, 2020 01:14
abbathaw pushed a commit to abbathaw/freeCodeCamp that referenced this pull request Jul 24, 2020
khal45 pushed a commit to khal45/failing-code-for-advancednode-and-express that referenced this pull request Jun 1, 2024
* update depds, remove unnecessary local files, update readme, package, server.  fixes {freeCodeCamp/freeCodeCamp/issuse/38404}

* Revise server.js to update instructions to match Challenge.md on main repo {freeCodeCamp/freeCodeCamp/pull/38572}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: curriculum Lessons, Challenges, Projects and other Curricular Content in curriculum directory.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants