Skip to content

Instantly share code, notes, and snippets.

@haldunatar
Last active February 18, 2018 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haldunatar/aadd28cc639642936f1e9bedfda7a2bd to your computer and use it in GitHub Desktop.
Save haldunatar/aadd28cc639642936f1e9bedfda7a2bd to your computer and use it in GitHub Desktop.
CRUD app for local frontend development.
const app = require('express')();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.listen(3000, () => console.log('server is running on 3000'));
mongoose.connect('mongodb://localhost:27017/todo-list');
const Schema = mongoose.Schema;
const todoShema = new Schema({
title: { type: String },
status: { type: Boolean }
});
const Todo = mongoose.model('Todo', todoShema);
const createResponse = (res, err, todos) => err ? res.send(err) : res.json(todos);
app.get('/rest/todo-list', (req, res) => Todo.find({}, createResponse.bind(null, res)));
app.post('/rest/todo-list', (req, res) => Todo.create(req.body, createResponse.bind(null, res)));
app.put('/rest/todo-list/:id', (req, res) => Todo.findByIdAndUpdate(req.params.id, req.body, createResponse.bind(null, res)));
app.delete('/rest/todo-list/:id', (req, res) => Todo.remove({_id: req.params.id}, createResponse.bind(null, res)));
app.delete('/rest/todo-list-all', (req, res) => Todo.remove({}, createResponse.bind(null, res)));
@haldunatar
Copy link
Author

haldunatar commented Dec 10, 2017

  • install MongoDb locally
  • npm i -D express mongoose body-parser
  • run server.js
    ... ready to use!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment