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

Project Express #34

Open
haydanu opened this issue Jul 12, 2019 · 4 comments
Open

Project Express #34

haydanu opened this issue Jul 12, 2019 · 4 comments

Comments

@haydanu
Copy link

haydanu commented Jul 12, 2019

  • Deploy to GitHub & Heroku
@haydanu
Copy link
Author

haydanu commented Jul 12, 2019

const express = require("express");
const app = express();
const bodyParser = require("body-parser");

const todoList = [
  {
    id: 1,
    task: "learn Express",
    done: false
  },
  {
    id: 2,
    task: "learn Express-Generator",
    done: false
  }
];

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());

//get all todo list
app.get("/", (req, res) => {
  res.send(todoList);
});

app.get("/:id", (req, res) => {
  try {
    const filteredTodo = todoList.find(item => item.id == req.params.id);
    res.send({
      message: "Here is what you looking for",
      filteredTodo
    });
  } catch (error) {
    res.send(error);
  }
});

// add new todo
app.post("/", (req, res) => {
  try {
    let newId = todoList.length + 1;
    let newTodo = {
      id: newId,
      task: req.body.task,
      done: false
    };

    todoList.push(newTodo);

    res.status(200).send({
      message: "todo successfully added",
      todoList
    });
  } catch (error) {
    res.send(error);
  }
});

// ~delete todo by its id~
app.delete("/:id", (req, res) => {
  try {
    const idToDelete = req.params.id;
    let newTodo = todoList.filter(item => item.id !== parseInt(idToDelete));

    todoList = newTodo;

    res.status(200).send(todoList);
  } catch (error) {
    console.log(error);
    res.send(error);
  }
});

// ~update a todo by its id~
app.put("/:id", (req, res) => {
  try {
    let getTodoToUpdate = todoList.findIndex(data => data.id == req.params.id);

    todoList.map(data => {
      if (data.id == req.params.id) {
        todoList[getTodoToUpdate].task = req.body.task;
      }
    });
    res.send({
      message: "data successfully updated",
      todoList
    });
  } catch (error) {
    res.send(error);
  }
});

app.listen(process.env.PORT || 3000, () => {
  console.log("Your server is running on PORT " + process.env.PORT);
});

@aditilham
Copy link

https://github.com/aditilham/project-express

@trie168
Copy link

trie168 commented Jul 12, 2019

https://github.com/trie168/036-project-express-basic

@krowter
Copy link

krowter commented Jul 15, 2019

https://github.com/krowter/basic-express

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants