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

Get JSON data #4

Closed
github-learning-lab bot opened this issue Aug 18, 2022 · 1 comment
Closed

Get JSON data #4

github-learning-lab bot opened this issue Aug 18, 2022 · 1 comment

Comments

@github-learning-lab
Copy link

Now that our server is listening for requests being made on localhost:8000 let's return some mock JSON data. Add the following to your server.js file:

const mockUserData=[
{name:'Mark'},
{name:'Jill'}
]
app.get('/users', function(req,res){
 	res.json({
 	 	success: true,
 	 	message: 'successfully got users. Nice!',
 	 	users: mockUserData
 	})
})

Overall your file should look like this:

const express = require('express');
const app = express();

const mockUserData=[
	{name:'Mark'},
	{name:'Jill'}
]

app.get('/users',function(req,res){
	res.json({
		success: true,
		message: 'successfully got users. Nice!',
		users: mockUserData
	})
})

app.listen(8000,function(){console.log('server is listening')})

Let's save your changes on GitHub:

git add server.js
git commit -m"add first GET route"
git push origin master
@github-learning-lab
Copy link
Author

You just made your first endpoint! This function will respond to a GET request at http://localhost:8000/users with a JSON file, which includes our mockData under the key 'users'. Let's test it out!

Restart your server (CTRL+C, then run node server.js) since we changed the file. Open a browser and navigate to http://localhost:8000/users

You should see a JSON file, served up from your terminal!💁‍

If not, make sure your repository looks like this.

Click here for the next step

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