Created
March 25, 2017 23:08
-
-
Save ybouhjira/a51396422324376f92f0f93e8469bb7e to your computer and use it in GitHub Desktop.
todos.js
This file contains 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 cors = require('cors'); | |
const bodyParser = require('body-parser'); | |
const app = express(); | |
app.use(cors()); | |
app.use(bodyParser.json()); | |
const todos = []; | |
app.get('/todos', (req, res) => { | |
res.json(todos); | |
}); | |
app.post('/todos', (req, res) => { | |
todos.push(req.body); | |
res.status(201).end(); | |
}); | |
app.delete('/todos/:id', (req, res) => { | |
console.log(req.params.id) | |
todos.splice(req.params.id, 1); | |
res.status(204).end(); | |
}); | |
app.put('/todos/:id', (req, res) => { | |
todos[req.params.id] = req.body; | |
res.status(204).end(); | |
}); | |
const port = process.argv[2]; | |
app.listen(port, () => { | |
console.log(`Started server on port ${port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment