diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2707b00 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/node_modules +yarn.lock \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..979709e --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/playground/mongo-db-connect.js b/playground/mongo-db-connect.js new file mode 100644 index 0000000..22460aa --- /dev/null +++ b/playground/mongo-db-connect.js @@ -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(); +}); \ No newline at end of file diff --git a/server/db/mongoose.js b/server/db/mongoose.js new file mode 100644 index 0000000..ea9fe1c --- /dev/null +++ b/server/db/mongoose.js @@ -0,0 +1,6 @@ +const mongoose = require('mongoose'); + +mongoose.Promise = global.Promise; +mongoose.connect('mongodb://localhost:27017/TodoApp', { useNewUrlParser: true }); + +module.exports = { mongoose }; \ No newline at end of file diff --git a/server/models/todo.js b/server/models/todo.js new file mode 100644 index 0000000..0ab5d61 --- /dev/null +++ b/server/models/todo.js @@ -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 }; \ No newline at end of file diff --git a/server/models/user.js b/server/models/user.js new file mode 100644 index 0000000..78f962b --- /dev/null +++ b/server/models/user.js @@ -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 }; \ No newline at end of file diff --git a/server/server.js b/server/server.js new file mode 100644 index 0000000..2435d26 --- /dev/null +++ b/server/server.js @@ -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'); +}); \ No newline at end of file