-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9482495
Showing
7 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/node_modules | ||
yarn.lock |
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
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" | ||
} | ||
} |
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
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(); | ||
}); |
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
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 }; |
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
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 }; |
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
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 }; |
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
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'); | ||
}); |