Skip to content
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

Use Variables in URL #5

Open
github-learning-lab bot opened this issue Feb 3, 2021 · 0 comments
Open

Use Variables in URL #5

github-learning-lab bot opened this issue Feb 3, 2021 · 0 comments

Comments

@github-learning-lab
Copy link

In Express, words with a colon in front of them in the url are treated as variables. You can access the value of each variable through req.params, like this:

app.get('/users/:id',function(req,res){
	console.log(req.params.id)
	res.json({
		success: true,
		message: 'got one user',
		user: req.params.id
	})
})

Let's test it out!

Add the function above your app.listen function in your server.js file, restart the server, and paste this into your browser url: http://localhost:8000/users/mark

Now check the terminal for the console.log of req.params.id. You should see the name 'mark', since it is the value we passed in place of the 'id' variable. Since we are running this in the terminal, our console.log will appear there (instead of the browser).

After testing that it works, push your changes your GitHub repo:

git add server.js
git commit -m"add dynamic GET route"
git push origin master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants