-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
39 lines (28 loc) · 747 Bytes
/
server.js
File metadata and controls
39 lines (28 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const express = require ('express');
const app = express();
require('dotenv').config();
const port = process.env.PORT || 8000
// app.get("/", (req,res) => {
// res.send("hello world")
// })
// app.get('/about', (req,res) => {
// res.send("thiq is the about page")
// })
let posts = [
{id: 1, title: 'post One'},
{id: 2, title: 'post Two'},
{id: 3, title: 'post Three'}
];
// get all posts
app.get('/api/posts', (req,res) =>{
res.json(posts);
})
// get a single post by id
app.get('/api/posts/:id', (req,res) =>{
console.log(req.params.id)
const id = parseInt(req.params.id)
res.json( posts.filter(post=>
post.id === id
))
})
app.listen(port, ()=> console.log(`server running on port ${port}`));