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

added code #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,49 @@ const app = express();

const PORT = 8080;

// Server our index.html
app.use(express.static("public"));

// GET - / - returns homepage
app.get('/', (req, res) => {
// serve up the public folder as static index.html file

res.sendFile(__dirname + "index.html");
});

// hello world route
app.get('/api', (req, res) => {
res.send('Hello World!');
res.send("<h1>Hello, World</h1>");
});

// get all pets from the database
app.get('/api/v1/pets', (req, res) => {
// send the pets array as a response

console.log(pets);
res.json(pets);
});

// get pet by owner with query string
app.get('/api/v1/pets/owner', (req, res) => {
// get the owner from the request

const owner= req.query.owner;

// find the pet in the pets array
const pet = pets.find(pet => pet.owner === owner);

// send the pet as a response

res.send(pet);
});

// get pet by name
app.get('/api/v1/pets/:name', (req, res) => {
// get the name from the request

const name= req.query.name;

// find the pet in the pets array
const pet = pets.find(pet => pet.name === name);

// send the pet as a response

res.send(pet);
});

app.listen(PORT, () => {
Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Document</title>
<title>Welcome to Pet Finder</title>
</head>
<body>
<h1>Pet Finder</h1>
Expand Down