-
-
Save Chojiu15/87e1d1d2a44d9b3d928b0a73dcade670 to your computer and use it in GitHub Desktop.
SuperHeroesApi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const superHeroesRouter = require('express').Router() | |
superHeroesRouter.use(require('express').json()) | |
const superHeroes = [ | |
{ id: 1, name: "Batman" }, | |
{ id: 2, name: "Spiderman" }, | |
{ id: 3, name: "BlackPanther" }, | |
]; | |
superHeroesRouter.get("/", (req, res) => { | |
res.send("Hello to my API").status(200); | |
}); | |
superHeroesRouter.get('/api/heroes', (req, res) => { | |
res.send(superHeroes) | |
}) | |
superHeroesRouter.get('/api/heroes/:id', (req, res) => { | |
let {id} = req.params | |
const hero = superHeroes.find(e => e.id === parseInt(id)) | |
res.send(hero) | |
}) | |
superHeroesRouter.post('/api/heroes', (req, res) => { | |
const hero = { | |
id : superHeroes.length + 1, | |
name : req.body.name | |
} | |
superHeroes.push(hero) | |
res.send(superHeroes) | |
}) | |
superHeroesRouter.put('/api/heroes/:id', (req, res) => { | |
let {id} = req.params | |
const hero = superHeroes.find(e => e.id === parseInt(id)) | |
hero.name = req.body.name | |
res.send(hero) | |
}) | |
superHeroesRouter.delete('/api/heroes/:id', (req, res) => { | |
let {id} = req.params | |
const hero = superHeroes.find(e => e.id === parseInt(id)) | |
const index = superHeroes.indexOf(hero) | |
superHeroes.splice(index, 1) | |
res.send('My hero as been deleted') | |
}) | |
module.exports = superHeroesRouter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const app = express(); | |
app.use(express.json()) | |
const connexion = require('./conf/conf') | |
const superHeroesRouter = require('./routes/superheroes') | |
app.use('/', superHeroesRouter) | |
app.listen(3002, console.log(`Server is running`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment