Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DeadlockDruid committed Sep 24, 2018
0 parents commit 9482495
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
yarn.lock
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "todo-api-node",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.3",
"mongodb": "^3.1.6",
"mongoose": "^5.2.17"
}
}
23 changes: 23 additions & 0 deletions playground/mongo-db-connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://localhost:27017/TodoApp', { useNewUrlParser: true }, (err, client) => {
var db = client.db('TodoApp');

if (err) {
return console.log('Unable to connect to MongoDb server.');
}

console.log('Connected to MongoDB server.');

db.collection('Todos').insertOne({
text: 'Something to do',
completed: 'false'
}, (err, result) => {
if (err) {
return console.log('Unable to insert todo.', err);
}
console.log(JSON.stringify(result.ops), undefined, 2);
});

client.close();
});
6 changes: 6 additions & 0 deletions server/db/mongoose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const mongoose = require('mongoose');

mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/TodoApp', { useNewUrlParser: true });

module.exports = { mongoose };
20 changes: 20 additions & 0 deletions server/models/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let mongoose = require('mongoose');

let Todo = mongoose.model('Todo', {
text: {
type: String,
required: true,
minlength: 3,
trim: true
},
completed: {
type: Boolean,
default: false
},
completed_at: {
type: Number,
default: null
}
});

module.exports = { Todo };
12 changes: 12 additions & 0 deletions server/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let mongoose = require('mongoose');

let User = mongoose.model('User', {
email: {
type: String,
required: true,
minlength: 5,
trim: true
}
});

module.exports = { User };
26 changes: 26 additions & 0 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
let express = require('express');
let bodyParser = require('body-parser');

let { mongoose } = require('./db/mongoose');
let { Todo } = require('./models/todo');
let { User } = require('./models/user');

let app = express();

app.use(bodyParser.json());

app.post('/todos', (req, res) => {
let todo = new Todo({
text: req.body.text
});

todo.save().then((doc) => {
res.send(doc);
}, (e) => {
res.status(400).send(e);
});
});

app.listen(3000, () => {
console.log('Started on port: 3000');
});

0 comments on commit 9482495

Please sign in to comment.