From bd1fe5f213e972de2cdd79ea8036b870eb345105 Mon Sep 17 00:00:00 2001 From: Divyanshu Rathore Date: Mon, 24 Sep 2018 20:02:11 +0530 Subject: [PATCH] Changes for heroku deployment --- package.json | 4 ++++ server/server.js | 22 ++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 59dfb81..fb07d94 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,13 @@ "description": "", "main": "index.js", "scripts": { + "start": "node server/server.js", "test": "mocha server/**/*.test.js", "test-watch": "nodemon --exec 'npm test'" }, + "engines": { + "node": "10.11.0" + }, "author": "", "license": "ISC", "dependencies": { diff --git a/server/server.js b/server/server.js index afa92a8..e4ad3ad 100644 --- a/server/server.js +++ b/server/server.js @@ -1,11 +1,13 @@ let express = require('express'); let bodyParser = require('body-parser'); +let { ObjectID } = require('mongodb'); let { mongoose } = require('./db/mongoose'); let { Todo } = require('./models/todo'); let { User } = require('./models/user'); let app = express(); +let port = process.env.PORT || 3000; app.use(bodyParser.json()); @@ -27,8 +29,24 @@ app.get('/todos', (req, res) => { }, (e) => res.status(400).send(e)); }); -app.listen(3000, () => { - console.log('Started on port: 3000'); +app.get('/todos/:id', (req, res) => { + let id = req.params.id; + + if (!ObjectID.isValid(id)) { + return res.status(404).send(); + } + + Todo.findById(id).then((todo) => { + if (!todo) { + return res.status(404).send(); + } + + res.send(todo); + }).catch((e) => res.status(400).send()); +}); + +app.listen(port, () => { + console.log(`Started on port: ${port}`); }); module.exports = { app }; \ No newline at end of file