rkjayko / node-express-course

An introduction to Node.js and Express.js servers
https://lab.github.com/everydeveloper/introduction-to-node-with-express
0 stars 0 forks source link

Get JSON data #5

Closed github-learning-lab[bot] closed 4 years ago

github-learning-lab[bot] commented 4 years ago

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[bot] commented 4 years ago

With this last push, your repository should look like this.

💡 You can use dynamic variables to search for specific data associated with an id in your database, and return that (instead of just returning the id).

Click here for the next step