From e4769bf9cce56e148d9c5ecbb7053b8bab24a44d Mon Sep 17 00:00:00 2001 From: Santan24 Date: Thu, 10 Jun 2021 12:28:52 +0530 Subject: [PATCH 01/19] commit --- middlewares/requireCredits.js | 6 ++++++ middlewares/requireLogin.js | 8 ++++++++ models/recipient.js | 14 ++++++++++++++ models/survey.js | 2 +- 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 middlewares/requireCredits.js create mode 100644 middlewares/requireLogin.js create mode 100644 models/recipient.js diff --git a/middlewares/requireCredits.js b/middlewares/requireCredits.js new file mode 100644 index 0000000..2093acf --- /dev/null +++ b/middlewares/requireCredits.js @@ -0,0 +1,6 @@ +module.exports = (req, res, next) => { + if (!req.user.credits <1) { + return res.status(403).send({ error: " You do Not have enough Credits!" }); + } + next(); +}; diff --git a/middlewares/requireLogin.js b/middlewares/requireLogin.js new file mode 100644 index 0000000..09fa251 --- /dev/null +++ b/middlewares/requireLogin.js @@ -0,0 +1,8 @@ +module.exports = (req, res, next) => { + if (!req.user) { + return res.status(401).send({ error: 'You must log in!' }); + } + + next(); + }; + \ No newline at end of file diff --git a/models/recipient.js b/models/recipient.js new file mode 100644 index 0000000..5429923 --- /dev/null +++ b/models/recipient.js @@ -0,0 +1,14 @@ +const mongoose= require('mongoose') + +const recipientSchema=mongoose.Schema({ + email: + { + type: String + }, + responded:{ + type:Boolean,default:false + } +}) + +const Recipient = mongoose.model("Recipient", recipientSchema); +module.exports = {Recipient}; diff --git a/models/survey.js b/models/survey.js index 1b56b7f..03486fc 100644 --- a/models/survey.js +++ b/models/survey.js @@ -21,6 +21,6 @@ const surveySchema=mongoose.Schema({ ] }) -//created the survey api now + const Survey = mongoose.model("Survey",surveySchema) module.exports={Survey} From 55b2ca8c4f4286ccd4490519827d0eb3967df1f1 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Thu, 10 Jun 2021 13:12:22 +0530 Subject: [PATCH 02/19] survey recipient models added --- app.js | 4 +++ models/recipient.js | 19 ++++++-------- models/survey.js | 60 +++++++++++++++++++++++++++------------------ routes/survey.js | 26 +++++++++++++++++--- 4 files changed, 70 insertions(+), 39 deletions(-) diff --git a/app.js b/app.js index 426de16..c74d533 100644 --- a/app.js +++ b/app.js @@ -4,6 +4,10 @@ const cors = require("cors"); const bcryptjs = require("bcryptjs"); require("dotenv").config(); require("./db/connectionDB"); + +require('./models/User') +require('./models/Survey') + const authRoutes = require("./routes/auth"); const surveyRoutes = require("./routes/survey"); diff --git a/models/recipient.js b/models/recipient.js index 5429923..992a4b4 100644 --- a/models/recipient.js +++ b/models/recipient.js @@ -1,14 +1,9 @@ -const mongoose= require('mongoose') +const mongoose = require('mongoose'); +const { Schema } = mongoose; -const recipientSchema=mongoose.Schema({ - email: - { - type: String - }, - responded:{ - type:Boolean,default:false - } -}) +const recipientSchema = new Schema({ + email: String, + responded: { type: Boolean, default: false } +}); -const Recipient = mongoose.model("Recipient", recipientSchema); -module.exports = {Recipient}; +module.exports = recipientSchema; diff --git a/models/survey.js b/models/survey.js index 03486fc..0ae4371 100644 --- a/models/survey.js +++ b/models/survey.js @@ -1,26 +1,38 @@ +const mongoose = require("mongoose"); +const recipientSchema= require('./recipient') +const surveySchema = mongoose.Schema({ + title: { + type: String, + }, + category: { + type: String, + }, + body: { + type: String, + }, + subject: { + type: String, + }, + recipients: + [ + recipientSchema + ], + yes: { + type: Number, + default: 0, + }, + No: { + type: Number, + default: 0, + }, +// _user:{ + // type:mongoose.Schema.Types.ObjectId, + // ref:'User},' + //relationship between survey and an individual user + dateSent: Date, + lastResponded: Date +}); -const mongoose=require('mongoose') -const surveySchema=mongoose.Schema({ - title:{ - type:String - }, - type:{ - type:String - }, - - questionsSet:[ - { - ques:{ - type: String - }, - ans:{ - type: [Boolean] - } - } -] - -}) - -const Survey = mongoose.model("Survey",surveySchema) -module.exports={Survey} +const Survey = mongoose.model("Survey", surveySchema); +module.exports = { Survey }; diff --git a/routes/survey.js b/routes/survey.js index 3b48b47..0fdad6c 100644 --- a/routes/survey.js +++ b/routes/survey.js @@ -1,7 +1,27 @@ const express = require("express"); -const { createSurvey } = require("../controllers/survey"); -const router = express.Router(); +const mongoose = require("mongoose"); +const requireLogin = require("../middlewares/requireLogin"); +const requireCredits = require("../middlewares/requireCredits"); +// const { createSurvey } = require("../controllers/survey"); +// const router = express.Router(); +// router.post("/createsurvey", createSurvey); +const Survey = mongoose.model("surveys"); +//first need to cheak user is actually logged in +module.exports = (app) => { + app.post("api/surveys", requireLogin, requireCredits, (req, res) => { + const { title, category, body, recipients } = req.body; -router.post("/createsurvey", createSurvey); + const survey = new Survey({ + title, + category, + subject, + body, + recipients: recipients.split(',').map(email => ({ email: email.trim() })), + _user: req.user.id, + dateSent: Date.now() + + }); + }); +}; module.exports = router; From 3009f9f27c1cb119a08109847a905c8f4d309056 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Thu, 10 Jun 2021 16:44:41 +0530 Subject: [PATCH 03/19] Added middleware --- models/survey.js | 17 +++++++---------- models/user.js | 5 +---- routes/survey.js | 11 +++++------ 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/models/survey.js b/models/survey.js index 0ae4371..9df0dbf 100644 --- a/models/survey.js +++ b/models/survey.js @@ -13,9 +13,8 @@ const surveySchema = mongoose.Schema({ subject: { type: String, }, - recipients: - [ - recipientSchema + recipients:[ + recipientSchema ], yes: { type: Number, @@ -25,14 +24,12 @@ const surveySchema = mongoose.Schema({ type: Number, default: 0, }, -// _user:{ - // type:mongoose.Schema.Types.ObjectId, - // ref:'User},' - //relationship between survey and an individual user + _user:{ + type:mongoose.Schema.Types.ObjectId, + ref:'User' + }, dateSent: Date, lastResponded: Date }); - - -const Survey = mongoose.model("Survey", surveySchema); +const Survey = mongoose.models.Survey||mongoose.model("Survey", surveySchema); module.exports = { Survey }; diff --git a/models/user.js b/models/user.js index eb7de48..04f831e 100644 --- a/models/user.js +++ b/models/user.js @@ -1,10 +1,8 @@ const mongoose = require("mongoose"); - const userSchema = mongoose.Schema({ username: { type: String, required: true, - max: 64, }, password: { type: String, @@ -30,6 +28,5 @@ const userSchema = mongoose.Schema({ default: null, }, }); - -const User = mongoose.model("User", userSchema); +const User = mongoose.models.User || mongoose.model("User", userSchema); module.exports = { User }; diff --git a/routes/survey.js b/routes/survey.js index 0fdad6c..890e51e 100644 --- a/routes/survey.js +++ b/routes/survey.js @@ -2,15 +2,15 @@ const express = require("express"); const mongoose = require("mongoose"); const requireLogin = require("../middlewares/requireLogin"); const requireCredits = require("../middlewares/requireCredits"); +const {Survey} = require('../models/survey') // const { createSurvey } = require("../controllers/survey"); // const router = express.Router(); // router.post("/createsurvey", createSurvey); -const Survey = mongoose.model("surveys"); //first need to cheak user is actually logged in -module.exports = (app) => { + +module.exports = (app) => { app.post("api/surveys", requireLogin, requireCredits, (req, res) => { const { title, category, body, recipients } = req.body; - const survey = new Survey({ title, category, @@ -19,9 +19,8 @@ module.exports = (app) => { recipients: recipients.split(',').map(email => ({ email: email.trim() })), _user: req.user.id, dateSent: Date.now() - }); + survey.save() }); }; - -module.exports = router; +// module.exports = router; From 08f0559d29bca774597741041d2a9e2e35f08bf0 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Thu, 10 Jun 2021 20:20:48 +0530 Subject: [PATCH 04/19] comments removed --- routes/survey.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/routes/survey.js b/routes/survey.js index 890e51e..5f793bd 100644 --- a/routes/survey.js +++ b/routes/survey.js @@ -2,13 +2,9 @@ const express = require("express"); const mongoose = require("mongoose"); const requireLogin = require("../middlewares/requireLogin"); const requireCredits = require("../middlewares/requireCredits"); -const {Survey} = require('../models/survey') -// const { createSurvey } = require("../controllers/survey"); -// const router = express.Router(); -// router.post("/createsurvey", createSurvey); -//first need to cheak user is actually logged in +const { Survey } = require("../models/survey"); -module.exports = (app) => { +module.exports = (app) => { app.post("api/surveys", requireLogin, requireCredits, (req, res) => { const { title, category, body, recipients } = req.body; const survey = new Survey({ @@ -16,11 +12,12 @@ module.exports = (app) => { category, subject, body, - recipients: recipients.split(',').map(email => ({ email: email.trim() })), + recipients: recipients + .split(",") + .map((email) => ({ email: email.trim() })), _user: req.user.id, - dateSent: Date.now() + dateSent: Date.now(), }); - survey.save() + survey.save(); }); }; -// module.exports = router; From 89081f2084ae9c44a657bbb8cdc4c917420b4c84 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Fri, 11 Jun 2021 00:05:14 +0530 Subject: [PATCH 05/19] removed extra spaces --- app.js | 3 --- models/recipient.js | 5 ++--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/app.js b/app.js index c74d533..2d9cb3c 100644 --- a/app.js +++ b/app.js @@ -4,15 +4,12 @@ const cors = require("cors"); const bcryptjs = require("bcryptjs"); require("dotenv").config(); require("./db/connectionDB"); - require('./models/User') require('./models/Survey') - const authRoutes = require("./routes/auth"); const surveyRoutes = require("./routes/survey"); const app = express(); - app.use(express.urlencoded({ extended: true })); app.use(express.json()); app.use(cors()); diff --git a/models/recipient.js b/models/recipient.js index 992a4b4..f4a63f1 100644 --- a/models/recipient.js +++ b/models/recipient.js @@ -1,9 +1,8 @@ -const mongoose = require('mongoose'); +const mongoose = require("mongoose"); const { Schema } = mongoose; - const recipientSchema = new Schema({ email: String, - responded: { type: Boolean, default: false } + responded: { type: Boolean, default: false }, }); module.exports = recipientSchema; From 27d5070933422df53b2dfb5dd8c75b0865b8dc64 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Fri, 11 Jun 2021 13:25:22 +0530 Subject: [PATCH 06/19] utils added --- controllers/auth.js | 8 +------- controllers/survey.js | 42 ++++++++++++++++++++++++++++++++++++++++-- models/survey.js | 2 +- routes/survey.js | 26 +++++--------------------- utils/transporter.js | 13 +++++++++++++ 5 files changed, 60 insertions(+), 31 deletions(-) create mode 100644 utils/transporter.js diff --git a/controllers/auth.js b/controllers/auth.js index a5b7317..f829a38 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -3,13 +3,7 @@ const nodemailer = require("nodemailer"); const bcryptjs = require("bcryptjs"); const jwt = require("jsonwebtoken"); -const transporter = nodemailer.createTransport({ - service: "gmail", - auth: { - user: "santanrathore75209@gmail.com", - pass: "*Saheb13*", - }, -}); +const {transporter} = require('../utils/transporter') const signup = async (req, res) => { const { username, password, email, phoneNo } = req.body; diff --git a/controllers/survey.js b/controllers/survey.js index 9693089..00cdad6 100644 --- a/controllers/survey.js +++ b/controllers/survey.js @@ -1,8 +1,46 @@ const mongoose = require("mongoose"); const { Survey } = require("../models/survey"); +const {transporter} = require('../utils/transporter') const createSurvey = async (req, res) => { - const survey = await Survey.create(req.body); + const { title, category,subject, body, recipients } = req.body; + const survey = await Survey.create({ + title, + category, + subject, + body, + recipients, + dateSent: Date.now(), + }); + + const data = { + from: "no-reply@gmail.com", + to: recipients, + subject: "Account activation link", + html: ` + +
+

I'd like your input!

+

Please answer the following question:

+

${survey.body}

+
+ Yes +
+
+ No +
+
+ + + `, + }; + + try { + await transporter.sendMail(data); + console.log("Email sent Successfully! survey"); + } catch (err) { + console.log("error occured while sending email!", err); + } res.send(survey); }; -module.exports = { createSurvey }; +module.exports = { createSurvey }; \ No newline at end of file diff --git a/models/survey.js b/models/survey.js index 9df0dbf..63931d9 100644 --- a/models/survey.js +++ b/models/survey.js @@ -14,7 +14,7 @@ const surveySchema = mongoose.Schema({ type: String, }, recipients:[ - recipientSchema + String ], yes: { type: Number, diff --git a/routes/survey.js b/routes/survey.js index 5f793bd..9ec0e1c 100644 --- a/routes/survey.js +++ b/routes/survey.js @@ -1,23 +1,7 @@ const express = require("express"); -const mongoose = require("mongoose"); -const requireLogin = require("../middlewares/requireLogin"); -const requireCredits = require("../middlewares/requireCredits"); -const { Survey } = require("../models/survey"); +const router = express.Router(); +const { createSurvey } = require("../controllers/survey"); -module.exports = (app) => { - app.post("api/surveys", requireLogin, requireCredits, (req, res) => { - const { title, category, body, recipients } = req.body; - const survey = new Survey({ - title, - category, - subject, - body, - recipients: recipients - .split(",") - .map((email) => ({ email: email.trim() })), - _user: req.user.id, - dateSent: Date.now(), - }); - survey.save(); - }); -}; +router.post("/createsurvey", createSurvey); + +module.exports = router; \ No newline at end of file diff --git a/utils/transporter.js b/utils/transporter.js new file mode 100644 index 0000000..bc6824d --- /dev/null +++ b/utils/transporter.js @@ -0,0 +1,13 @@ +const nodemailer = require('nodemailer') + +const transporter = nodemailer.createTransport({ + service: "gmail", + auth: { + user: "santanrathore75209@gmail.com", + pass: "*Saheb13*", + }, +}); + +module.exports ={ + transporter +} \ No newline at end of file From 5e132882d19fcd901e66e86a0562f82f4c304726 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Fri, 11 Jun 2021 13:35:35 +0530 Subject: [PATCH 07/19] corret user in app.js --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index 2d9cb3c..d83e4e6 100644 --- a/app.js +++ b/app.js @@ -4,7 +4,7 @@ const cors = require("cors"); const bcryptjs = require("bcryptjs"); require("dotenv").config(); require("./db/connectionDB"); -require('./models/User') +require('./models/user') require('./models/Survey') const authRoutes = require("./routes/auth"); const surveyRoutes = require("./routes/survey"); From b455df2ed40a561db0143bc46868b813c8930bc6 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Fri, 11 Jun 2021 21:14:15 +0530 Subject: [PATCH 08/19] Activation link correction --- controllers/auth.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/auth.js b/controllers/auth.js index f829a38..ba6fa97 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -27,7 +27,7 @@ const signup = async (req, res) => { from: "no-reply@gmail.com", to: email, subject: "Account activation link", - html: `

PLease click on given link to activate your account

+ html: `

Account activation link

http://localhost:2000/api/activate/${token}

`, }; From 71be255cb37ebddb4d1f424eb523f03942ee0c41 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Mon, 14 Jun 2021 21:08:04 +0530 Subject: [PATCH 09/19] feedback retrieving --- controllers/auth.js | 16 +++++++---- controllers/survey.js | 57 +++++++++++++++++++++++++++++++++---- middlewares/requireLogin.js | 27 +++++++++++++----- models/survey.js | 1 + routes/auth.js | 4 +-- routes/survey.js | 9 ++++-- 6 files changed, 90 insertions(+), 24 deletions(-) diff --git a/controllers/auth.js b/controllers/auth.js index ba6fa97..393b91d 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -27,9 +27,14 @@ const signup = async (req, res) => { from: "no-reply@gmail.com", to: email, subject: "Account activation link", - html: `

Account activation link

-

http://localhost:2000/api/activate/${token}

- `, + html: ` +
+

http://localhost:2000/api/activate/${token}">

+

Account activation link

+
+ Ask-FEED + + `, }; try { await transporter.sendMail(data); @@ -72,7 +77,6 @@ const verifyAccount = async (req, res) => { const resetlink = async (req, res) => { const { email } = req.body; const user = await User.findOne({ email }); - // console.log("user", user, email); if (user) { const token = jwt.sign({ email }, process.env.JWT_SECRET, { expiresIn: "30m", @@ -91,7 +95,7 @@ const resetlink = async (req, res) => { to: email, subject: "Reset link", html: `

PLease click on given link to reset your account

-

http://localhost:2000/api/changepassword/?token=${token}

+

http://localhost:2000/api/changepassword/${token}

`, }; try { @@ -107,7 +111,7 @@ const resetlink = async (req, res) => { const changepassword = async (req, res) => { const { password } = req.body; - const token = req.query.token; + const token = req.params.token; const decodedtoken = jwt.verify(token, process.env.JWT_SECRET); const hashedPass = await bcryptjs.hashSync(password, 10); const user = await User.findOneAndUpdate( diff --git a/controllers/survey.js b/controllers/survey.js index 00cdad6..576d709 100644 --- a/controllers/survey.js +++ b/controllers/survey.js @@ -1,15 +1,16 @@ const mongoose = require("mongoose"); const { Survey } = require("../models/survey"); -const {transporter} = require('../utils/transporter') +const { transporter } = require("../utils/transporter"); const createSurvey = async (req, res) => { - const { title, category,subject, body, recipients } = req.body; + const { title, category, subject, body, recipients } = req.body; const survey = await Survey.create({ title, category, subject, body, recipients, + dateSent: Date.now(), }); @@ -24,17 +25,17 @@ const createSurvey = async (req, res) => {

Please answer the following question:

${survey.body}

- Yes + Yes
- No + No
`, }; - + try { await transporter.sendMail(data); console.log("Email sent Successfully! survey"); @@ -43,4 +44,48 @@ const createSurvey = async (req, res) => { } res.send(survey); }; -module.exports = { createSurvey }; \ No newline at end of file + +const responseYes = async (req, res) => { + try { + const id = req.params.id; + + const dataToSet = { + $inc: { + yes: 1, + }, + }; + const result = await Survey.findByIdAndUpdate(id, dataToSet); + if (result) { + res.send("response recorded successfully"); + } + else + { + res.send("Could not record the respone") + } + } catch (e) { + throw new Error("Internal Serever Error"); + } +}; + +const responseNo = async (req, res) => { + try { + const id = req.params.id; + const dataToSet = { + $inc: { + No: 1, + }, + }; + const result = await Survey.findByIdAndUpdate(id, dataToSet); + if (result) { + res.send("response recorded successfully"); + } + else + { + res.send("Could not record the respone") + } + } catch (e) { + throw new Error("Internal Serever Error"); + } +}; + +module.exports = { createSurvey,responseYes, responseNo }; diff --git a/middlewares/requireLogin.js b/middlewares/requireLogin.js index 09fa251..6013d4f 100644 --- a/middlewares/requireLogin.js +++ b/middlewares/requireLogin.js @@ -1,8 +1,21 @@ -module.exports = (req, res, next) => { - if (!req.user) { - return res.status(401).send({ error: 'You must log in!' }); +const jwt = require("jsonwebtoken"); + +const { User } = require("../models/user"); +const protect = async (req, res, next) => { + try { + const token = req.headers.authorization.split(" ")[1]; + const user = jwt.verify(token, process.env.JWT_SECRET); + + const userExist = await User.findOne({ email: user.email }); + + if (userExist) { + req.user = userExist; + next(); + } else { + res.send("user does not exist"); } - - next(); - }; - \ No newline at end of file + } catch (e) { + res.send("invalid token"); + } +}; +module.exports = { protect }; diff --git a/models/survey.js b/models/survey.js index 63931d9..01e3d81 100644 --- a/models/survey.js +++ b/models/survey.js @@ -24,6 +24,7 @@ const surveySchema = mongoose.Schema({ type: Number, default: 0, }, + _user:{ type:mongoose.Schema.Types.ObjectId, ref:'User' diff --git a/routes/auth.js b/routes/auth.js index d1c6751..c48c5c2 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -10,9 +10,9 @@ const { } = require("../controllers/auth"); router.post("/signup", signup); -router.post("/activate", verifyAccount); +router.post("/activate/:token", verifyAccount); router.post("/resetlink", resetlink); -router.post("/changepassword", changepassword); +router.post("/changepassword/:token", changepassword); router.post("/login", login); module.exports = router; diff --git a/routes/survey.js b/routes/survey.js index 9ec0e1c..cd7ed36 100644 --- a/routes/survey.js +++ b/routes/survey.js @@ -1,7 +1,10 @@ +const { response } = require("express"); const express = require("express"); const router = express.Router(); -const { createSurvey } = require("../controllers/survey"); - -router.post("/createsurvey", createSurvey); +const {protect}=require('../middlewares/requireLogin') +const { createSurvey,responseYes, responseNo, getSurvey } = require("../controllers/survey"); +router.post("/createsurvey",protect,createSurvey); +router.put("/response/yes/:id",responseYes) +router.put("/response/no/:id",responseNo) module.exports = router; \ No newline at end of file From de157399bd6dfef4ade16c5d3c0e3dfb0a9ace17 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Tue, 15 Jun 2021 18:33:15 +0530 Subject: [PATCH 10/19] get survey and updated profile --- .env | 3 +- app.js | 4 +-- controllers/auth.js | 53 +++++++++++++++++++-------- controllers/survey.js | 28 ++++++++------- middlewares/requireCredits.js | 6 ---- models/survey.js | 68 ++++++++++++++++++----------------- routes/auth.js | 3 ++ routes/survey.js | 18 ++++++---- utils/transporter.js | 12 +++---- 9 files changed, 115 insertions(+), 80 deletions(-) delete mode 100644 middlewares/requireCredits.js diff --git a/.env b/.env index 4c24d8d..e24a93a 100644 --- a/.env +++ b/.env @@ -1,4 +1,5 @@ PORT=2000 JWT_SECRET=askfeed1234 - +USER=santanrathore75209@gmail.com +PASS=*Saheb13* diff --git a/app.js b/app.js index d83e4e6..84cc13c 100644 --- a/app.js +++ b/app.js @@ -4,8 +4,8 @@ const cors = require("cors"); const bcryptjs = require("bcryptjs"); require("dotenv").config(); require("./db/connectionDB"); -require('./models/user') -require('./models/Survey') +require("./models/user"); +require("./models/survey"); const authRoutes = require("./routes/auth"); const surveyRoutes = require("./routes/survey"); diff --git a/controllers/auth.js b/controllers/auth.js index 393b91d..1711cbe 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -3,7 +3,7 @@ const nodemailer = require("nodemailer"); const bcryptjs = require("bcryptjs"); const jwt = require("jsonwebtoken"); -const {transporter} = require('../utils/transporter') +const { transporter } = require("../utils/transporter"); const signup = async (req, res) => { const { username, password, email, phoneNo } = req.body; @@ -130,20 +130,45 @@ const changepassword = async (req, res) => { } }; const login = async (req, res) => { - const { email, password } = req.body; - const user = await User.findOne({ email }); - const { _id } = user; - if (user) { - console.log("user", user); - const matchpassword = await bcryptjs.compare(password, user.password); - if (matchpassword && user.isVarified) { - const token = jwt.sign({ email, _id }, process.env.JWT_SECRET); - res.send({ token }); + try { + const { email, password } = req.body; + const user = await User.findOne({ email }); + const { _id } = user; + if (user) { + console.log("user", user); + const matchpassword = await bcryptjs.compare(password, user.password); + if (matchpassword && user.isVarified) { + const token = jwt.sign({ email, _id }, process.env.JWT_SECRET); + res.send({ token }); + } else { + res.send("Login unsuccessful!"); + } } else { - res.send("Login unsuccessful!"); + res.send("Incorrect Email or password!"); } - } else { - res.send("Incorrect Email or password!"); + } catch (e) { + throw new ERROR("Fail to create operation"); } }; -module.exports = { signup, verifyAccount, resetlink, changepassword, login }; +const updateProfile = async (req, res) => { + try { + const { username, phoneNo, _id } = req.body; + const userexist = await User.findOneAndUpdate({ _id }, { $set: req.body }); + if (userexist) { + res.send("user updated successfully"); + } else { + res.send("unable to update"); + } + } catch (e) { + throw new ERROR("Unable to update"); + } +}; + +module.exports = { + signup, + verifyAccount, + resetlink, + changepassword, + login, + updateProfile, +}; diff --git a/controllers/survey.js b/controllers/survey.js index 576d709..064fb92 100644 --- a/controllers/survey.js +++ b/controllers/survey.js @@ -10,7 +10,7 @@ const createSurvey = async (req, res) => { subject, body, recipients, - + dateSent: Date.now(), }); @@ -47,7 +47,7 @@ const createSurvey = async (req, res) => { const responseYes = async (req, res) => { try { - const id = req.params.id; + const id = req.params.id; const dataToSet = { $inc: { @@ -57,10 +57,8 @@ const responseYes = async (req, res) => { const result = await Survey.findByIdAndUpdate(id, dataToSet); if (result) { res.send("response recorded successfully"); - } - else - { - res.send("Could not record the respone") + } else { + res.send("Could not record the respone"); } } catch (e) { throw new Error("Internal Serever Error"); @@ -69,7 +67,7 @@ const responseYes = async (req, res) => { const responseNo = async (req, res) => { try { - const id = req.params.id; + const id = req.params.id; const dataToSet = { $inc: { No: 1, @@ -78,14 +76,20 @@ const responseNo = async (req, res) => { const result = await Survey.findByIdAndUpdate(id, dataToSet); if (result) { res.send("response recorded successfully"); + } else { + res.send("Could not record the respone"); } - else - { - res.send("Could not record the respone") - } + } catch (e) { + throw new Error("Internal Serever Error"); + } +}; +const getSurvey = async (req, res) => { + try { + const surveys = await Survey.find(); + res.send(surveys); } catch (e) { throw new Error("Internal Serever Error"); } }; -module.exports = { createSurvey,responseYes, responseNo }; +module.exports = { createSurvey, responseYes, responseNo, getSurvey }; diff --git a/middlewares/requireCredits.js b/middlewares/requireCredits.js deleted file mode 100644 index 2093acf..0000000 --- a/middlewares/requireCredits.js +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = (req, res, next) => { - if (!req.user.credits <1) { - return res.status(403).send({ error: " You do Not have enough Credits!" }); - } - next(); -}; diff --git a/models/survey.js b/models/survey.js index 01e3d81..cefd7b4 100644 --- a/models/survey.js +++ b/models/survey.js @@ -1,36 +1,38 @@ const mongoose = require("mongoose"); -const recipientSchema= require('./recipient') -const surveySchema = mongoose.Schema({ - title: { - type: String, +const recipientSchema = require("./recipient"); +const surveySchema = mongoose.Schema( + { + title: { + type: String, + }, + category: { + type: String, + }, + body: { + type: String, + }, + subject: { + type: String, + }, + recipients: [String], + yes: { + type: Number, + default: 0, + }, + No: { + type: Number, + default: 0, + }, + + _user: { + type: mongoose.Schema.Types.ObjectId, + ref: "User", + }, + + dateSent: Date, + lastResponded: Date, }, - category: { - type: String, - }, - body: { - type: String, - }, - subject: { - type: String, - }, - recipients:[ - String - ], - yes: { - type: Number, - default: 0, - }, - No: { - type: Number, - default: 0, - }, - - _user:{ - type:mongoose.Schema.Types.ObjectId, - ref:'User' - }, - dateSent: Date, - lastResponded: Date -}); -const Survey = mongoose.models.Survey||mongoose.model("Survey", surveySchema); + { timestamps: true } +); +const Survey = mongoose.models.Survey || mongoose.model("Survey", surveySchema); module.exports = { Survey }; diff --git a/routes/auth.js b/routes/auth.js index c48c5c2..6532d2a 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -1,4 +1,5 @@ const express = require("express"); + const router = express.Router(); const { @@ -7,6 +8,7 @@ const { resetlink, changepassword, login, + updateProfile, } = require("../controllers/auth"); router.post("/signup", signup); @@ -14,5 +16,6 @@ router.post("/activate/:token", verifyAccount); router.post("/resetlink", resetlink); router.post("/changepassword/:token", changepassword); router.post("/login", login); +router.put("/updateprofile", updateProfile); module.exports = router; diff --git a/routes/survey.js b/routes/survey.js index cd7ed36..47bc1b0 100644 --- a/routes/survey.js +++ b/routes/survey.js @@ -1,10 +1,16 @@ const { response } = require("express"); const express = require("express"); const router = express.Router(); -const {protect}=require('../middlewares/requireLogin') -const { createSurvey,responseYes, responseNo, getSurvey } = require("../controllers/survey"); +const { protect } = require("../middlewares/requireLogin"); +const { + createSurvey, + responseYes, + responseNo, + getSurvey, +} = require("../controllers/survey"); -router.post("/createsurvey",protect,createSurvey); -router.put("/response/yes/:id",responseYes) -router.put("/response/no/:id",responseNo) -module.exports = router; \ No newline at end of file +router.post("/createsurvey", protect, createSurvey); +router.put("/response/yes/:id", responseYes); +router.put("/response/no/:id", responseNo); +router.get("/getSurvey", getSurvey); +module.exports = router; diff --git a/utils/transporter.js b/utils/transporter.js index bc6824d..3a7c1f1 100644 --- a/utils/transporter.js +++ b/utils/transporter.js @@ -1,13 +1,13 @@ -const nodemailer = require('nodemailer') +const nodemailer = require("nodemailer"); const transporter = nodemailer.createTransport({ service: "gmail", auth: { - user: "santanrathore75209@gmail.com", - pass: "*Saheb13*", + user: process.env.USER, + pass: process.env.PASS, }, }); -module.exports ={ - transporter -} \ No newline at end of file +module.exports = { + transporter, +}; From 4de8d8dad75c624d2d801a5a977edbc55897c0be Mon Sep 17 00:00:00 2001 From: Santan24 Date: Wed, 16 Jun 2021 21:01:55 +0530 Subject: [PATCH 11/19] Updated mail --- .env | 2 +- .gitignore | 1 + controllers/auth.js | 18 +++++---- controllers/images/askfeedlogo.jpeg | Bin 0 -> 7566 bytes controllers/survey.js | 4 +- models/survey.js | 55 ++++++++++------------------ 6 files changed, 34 insertions(+), 46 deletions(-) create mode 100644 controllers/images/askfeedlogo.jpeg diff --git a/.env b/.env index e24a93a..9fd6ce1 100644 --- a/.env +++ b/.env @@ -2,4 +2,4 @@ PORT=2000 JWT_SECRET=askfeed1234 USER=santanrathore75209@gmail.com -PASS=*Saheb13* +PASS=*Saheb13* \ No newline at end of file diff --git a/.gitignore b/.gitignore index 3c3629e..1dcef2d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.env \ No newline at end of file diff --git a/controllers/auth.js b/controllers/auth.js index 1711cbe..5f7a969 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -29,11 +29,12 @@ const signup = async (req, res) => { subject: "Account activation link", html: `
-

http://localhost:2000/api/activate/${token}">

-

Account activation link

-
- Ask-FEED - +

Click on the following button to activate your account

+

ACTIVATE ACCOUNT

+
+ Ask-FEED + + `, }; try { @@ -94,8 +95,11 @@ const resetlink = async (req, res) => { from: "no-reply@gmail.com", to: email, subject: "Reset link", - html: `

PLease click on given link to reset your account

-

http://localhost:2000/api/changepassword/${token}

+ html: `
+

PLease click on buuton to reset your password

+

RESET PASSWORD PASSWORD

+

+
`, }; try { diff --git a/controllers/images/askfeedlogo.jpeg b/controllers/images/askfeedlogo.jpeg new file mode 100644 index 0000000000000000000000000000000000000000..19aa679a9960f5e59a752418c004e74740e01109 GIT binary patch literal 7566 zcmb7IWl$Wny2?Yr$896xxIUzA66(u>_>mR-U0v2KGZ*iyZJkjQK!}gF{G+_izLQ6AK%FLxGEjPsu7srC{@jO*esnS_l@(u4wqG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850BD6(Hy)V4RDSw$OpMnfLC%PptRh9N=m~rdt;_y znO)FQaO?!bs!N#DSyNE0($#Rb=X0?6;U7ILc0 zJJbugj*HqHglYR7m-yf4+DD}xYv`oS9!dNqaS*s1OdlXmW-Ug;3~q2wESX0}M%Kt{#7AQbO)${{vMk BJjVb4 literal 0 HcmV?d00001 diff --git a/controllers/survey.js b/controllers/survey.js index 064fb92..650ec54 100644 --- a/controllers/survey.js +++ b/controllers/survey.js @@ -21,8 +21,8 @@ const createSurvey = async (req, res) => { html: `
-

I'd like your input!

-

Please answer the following question:

+

${survey.title}

+

${survey.subject}

${survey.body}

Yes diff --git a/models/survey.js b/models/survey.js index cefd7b4..6532d2a 100644 --- a/models/survey.js +++ b/models/survey.js @@ -1,38 +1,21 @@ -const mongoose = require("mongoose"); -const recipientSchema = require("./recipient"); -const surveySchema = mongoose.Schema( - { - title: { - type: String, - }, - category: { - type: String, - }, - body: { - type: String, - }, - subject: { - type: String, - }, - recipients: [String], - yes: { - type: Number, - default: 0, - }, - No: { - type: Number, - default: 0, - }, +const express = require("express"); - _user: { - type: mongoose.Schema.Types.ObjectId, - ref: "User", - }, +const router = express.Router(); - dateSent: Date, - lastResponded: Date, - }, - { timestamps: true } -); -const Survey = mongoose.models.Survey || mongoose.model("Survey", surveySchema); -module.exports = { Survey }; +const { + signup, + verifyAccount, + resetlink, + changepassword, + login, + updateProfile, +} = require("../controllers/auth"); + +router.post("/signup", signup); +router.post("/activate/:token", verifyAccount); +router.post("/resetlink", resetlink); +router.post("/changepassword/:token", changepassword); +router.post("/login", login); +router.put("/updateprofile", updateProfile); + +module.exports = router; From 221b6531f1a92da9ccfe419a36cb735ed48a1c80 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Wed, 16 Jun 2021 21:53:57 +0530 Subject: [PATCH 12/19] Added logo in mail --- controllers/auth.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/controllers/auth.js b/controllers/auth.js index 5f7a969..b51dff2 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -32,10 +32,19 @@ const signup = async (req, res) => {

Click on the following button to activate your account

ACTIVATE ACCOUNT


- Ask-FEED - - `, + + +
+
+ + `, + attachments: [{ + filename: 'askfeedlogo.jpeg', + path:`${__dirname}/images/askfeedlogo.jpeg`, + cid: 'askfeedlogo' + }] + }; try { await transporter.sendMail(data); @@ -100,7 +109,17 @@ const resetlink = async (req, res) => {

RESET PASSWORD PASSWORD


- `, + + + + + `, + attachments: [{ + filename: 'askfeedlogo.jpeg', + path:`${__dirname}/images/askfeedlogo.jpeg`, + cid: 'askfeedlogo' + }] + }; try { await transporter.sendMail(data); From 9db0fe784ee4a21802a7eded64873a7064d5ccfa Mon Sep 17 00:00:00 2001 From: Santan24 Date: Wed, 16 Jun 2021 22:36:56 +0530 Subject: [PATCH 13/19] changed spellings of please --- controllers/auth.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/controllers/auth.js b/controllers/auth.js index b51dff2..d511392 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -39,12 +39,13 @@ const signup = async (req, res) => { `, - attachments: [{ - filename: 'askfeedlogo.jpeg', - path:`${__dirname}/images/askfeedlogo.jpeg`, - cid: 'askfeedlogo' - }] - + attachments: [ + { + filename: "askfeedlogo.jpeg", + path: `${__dirname}/images/askfeedlogo.jpeg`, + cid: "askfeedlogo", + }, + ], }; try { await transporter.sendMail(data); @@ -105,7 +106,7 @@ const resetlink = async (req, res) => { to: email, subject: "Reset link", html: `
-

PLease click on buuton to reset your password

+

Please click on buton to reset your password

RESET PASSWORD PASSWORD


@@ -114,12 +115,13 @@ const resetlink = async (req, res) => { `, - attachments: [{ - filename: 'askfeedlogo.jpeg', - path:`${__dirname}/images/askfeedlogo.jpeg`, - cid: 'askfeedlogo' - }] - + attachments: [ + { + filename: "askfeedlogo.jpeg", + path: `${__dirname}/images/askfeedlogo.jpeg`, + cid: "askfeedlogo", + }, + ], }; try { await transporter.sendMail(data); From 1e7b685382636a89443c44f23080efbc4cf7a8b9 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Thu, 17 Jun 2021 17:45:56 +0530 Subject: [PATCH 14/19] Fixed bugs --- .env | 2 +- app.js | 4 +-- controllers/auth.js | 70 ++++++++++++++++++++++++------------------- controllers/survey.js | 12 ++++++-- models/survey.js | 54 ++++++++++++++++++++++----------- 5 files changed, 89 insertions(+), 53 deletions(-) diff --git a/.env b/.env index 9fd6ce1..a5b5ed0 100644 --- a/.env +++ b/.env @@ -1,5 +1,5 @@ -PORT=2000 +PORT=3000 JWT_SECRET=askfeed1234 USER=santanrathore75209@gmail.com PASS=*Saheb13* \ No newline at end of file diff --git a/app.js b/app.js index 84cc13c..aff2297 100644 --- a/app.js +++ b/app.js @@ -16,6 +16,6 @@ app.use(cors()); app.use("/survey", surveyRoutes); app.use("/api", authRoutes); -app.listen(process.env.PORT || 2000, () => { - console.log("server running on port 2000"); +app.listen(process.env.PORT || 3000, () => { + console.log("server running on port 3000"); }); diff --git a/controllers/auth.js b/controllers/auth.js index d511392..f83bce6 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -26,19 +26,27 @@ const signup = async (req, res) => { const data = { from: "no-reply@gmail.com", to: email, - subject: "Account activation link", - html: ` -
-

Click on the following button to activate your account

-

ACTIVATE ACCOUNT

-
- - - - - - - `, + subject: "This is your Email verification link", + html: `
+
+ +

Please click on the button to verify your mail!
We’re very excited to have you on board!

+ + +
+ +
+ + + + `, + attachments: [ + { + filename: "askfeedlogo.jpeg", + path: `${__dirname}/images/askfeedlogo.jpeg`, + cid: "askfeedlogo", + }, + ], attachments: [ { filename: "askfeedlogo.jpeg", @@ -104,17 +112,19 @@ const resetlink = async (req, res) => { const data = { from: "no-reply@gmail.com", to: email, - subject: "Reset link", + subject: "This is your account reset link", html: `
-

Please click on buton to reset your password

-

RESET PASSWORD PASSWORD

-

-
- - - - - `, +
+ +

This is your account reset link!
Please click on the link to reset your password!

+ +
+ +
+ + + + `, attachments: [ { filename: "askfeedlogo.jpeg", @@ -158,21 +168,21 @@ const login = async (req, res) => { try { const { email, password } = req.body; const user = await User.findOne({ email }); - const { _id } = user; + if (user) { - console.log("user", user); + const { _id } = user; + const matchpassword = await bcryptjs.compare(password, user.password); if (matchpassword && user.isVarified) { const token = jwt.sign({ email, _id }, process.env.JWT_SECRET); - res.send({ token }); - } else { - res.send("Login unsuccessful!"); + res.send({ token, email: user.email, username: user.username }); } } else { res.send("Incorrect Email or password!"); } + e; } catch (e) { - throw new ERROR("Fail to create operation"); + throw new Error("Fail to create operation"); } }; const updateProfile = async (req, res) => { @@ -185,7 +195,7 @@ const updateProfile = async (req, res) => { res.send("unable to update"); } } catch (e) { - throw new ERROR("Unable to update"); + throw new Error("Unable to update"); } }; diff --git a/controllers/survey.js b/controllers/survey.js index 650ec54..009bb9f 100644 --- a/controllers/survey.js +++ b/controllers/survey.js @@ -21,19 +21,27 @@ const createSurvey = async (req, res) => { html: `
+

${survey.title}

${survey.subject}

${survey.body}

- Yes + Yes
- No + No
`, + attachments: [ + { + filename: "askfeedlogo.jpeg", + path: `${__dirname}/images/askfeedlogo.jpeg`, + cid: "askfeedlogo", + }, + ], }; try { diff --git a/models/survey.js b/models/survey.js index 6532d2a..ad6a397 100644 --- a/models/survey.js +++ b/models/survey.js @@ -1,21 +1,39 @@ -const express = require("express"); +const mongoose = require("mongoose"); +const recipientSchema = require("./recipient"); -const router = express.Router(); +const surveySchema = mongoose.Schema( + { + title: { + type: String, + }, + category: { + type: String, + }, + body: { + type: String, + }, + subject: { + type: String, + }, + recipients: [String], + yes: { + type: Number, + default: 0, + }, + No: { + type: Number, + default: 0, + }, -const { - signup, - verifyAccount, - resetlink, - changepassword, - login, - updateProfile, -} = require("../controllers/auth"); + _user: { + type: mongoose.Schema.Types.ObjectId, + ref: "User", + }, -router.post("/signup", signup); -router.post("/activate/:token", verifyAccount); -router.post("/resetlink", resetlink); -router.post("/changepassword/:token", changepassword); -router.post("/login", login); -router.put("/updateprofile", updateProfile); - -module.exports = router; + dateSent: Date, + lastResponded: Date, + }, + { timestamps: true } +); +const Survey = mongoose.models.Survey || mongoose.model("Survey", surveySchema); +module.exports = { Survey }; From 9bb471e82a1d524dcc88d40f84383a92be9a5098 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Fri, 18 Jun 2021 21:33:04 +0530 Subject: [PATCH 15/19] Dockerfile created --- .dockerignore file | 2 ++ .scannerwork/.sonar_lock | 0 .scannerwork/report-task.txt | 6 ++++++ Dockerfile | 8 ++++++++ controllers/auth.js | 17 +++++++---------- controllers/survey.js | 2 +- 6 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 .dockerignore file create mode 100644 .scannerwork/.sonar_lock create mode 100644 .scannerwork/report-task.txt create mode 100644 Dockerfile diff --git a/.dockerignore file b/.dockerignore file new file mode 100644 index 0000000..5171c54 --- /dev/null +++ b/.dockerignore file @@ -0,0 +1,2 @@ +node_modules +npm-debug.log \ No newline at end of file diff --git a/.scannerwork/.sonar_lock b/.scannerwork/.sonar_lock new file mode 100644 index 0000000..e69de29 diff --git a/.scannerwork/report-task.txt b/.scannerwork/report-task.txt new file mode 100644 index 0000000..b851003 --- /dev/null +++ b/.scannerwork/report-task.txt @@ -0,0 +1,6 @@ +projectKey=token +serverUrl=http://dev-sonarqube.springrole.com:9000 +serverVersion=8.9.1.44547 +dashboardUrl=http://dev-sonarqube.springrole.com:9000/dashboard?id=token +ceTaskId=AXoaL8nL8zulQfXeM_9Q +ceTaskUrl=http://dev-sonarqube.springrole.com:9000/api/ce/task?id=AXoaL8nL8zulQfXeM_9Q diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d75b524 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM node:alpine +ENV PORT 3000 +RUN mkdir -p /usr/app +WORKDIR /usr/app +COPY package*.json /usr/app/ +RUN npm install +COPY . /usr/app +CMD ["npm","start"] \ No newline at end of file diff --git a/controllers/auth.js b/controllers/auth.js index f83bce6..c46e164 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -6,6 +6,7 @@ const jwt = require("jsonwebtoken"); const { transporter } = require("../utils/transporter"); const signup = async (req, res) => { + try{ const { username, password, email, phoneNo } = req.body; const userexist = await User.findOne({ email }); if (!userexist) { @@ -20,7 +21,6 @@ const signup = async (req, res) => { phoneNo, token, }; - const user = await User.create(newUser); if (user) { const data = { @@ -47,13 +47,6 @@ const signup = async (req, res) => { cid: "askfeedlogo", }, ], - attachments: [ - { - filename: "askfeedlogo.jpeg", - path: `${__dirname}/images/askfeedlogo.jpeg`, - cid: "askfeedlogo", - }, - ], }; try { await transporter.sendMail(data); @@ -66,6 +59,11 @@ const signup = async (req, res) => { } else { res.send("User already exist!"); } +} +catch(e) +{ + throw new Error("Failed to create the function") +} }; const verifyAccount = async (req, res) => { @@ -180,14 +178,13 @@ const login = async (req, res) => { } else { res.send("Incorrect Email or password!"); } - e; } catch (e) { throw new Error("Fail to create operation"); } }; const updateProfile = async (req, res) => { try { - const { username, phoneNo, _id } = req.body; + const { _id } = req.body; const userexist = await User.findOneAndUpdate({ _id }, { $set: req.body }); if (userexist) { res.send("user updated successfully"); diff --git a/controllers/survey.js b/controllers/survey.js index 009bb9f..9c55e1c 100644 --- a/controllers/survey.js +++ b/controllers/survey.js @@ -61,7 +61,7 @@ const responseYes = async (req, res) => { $inc: { yes: 1, }, - }; + } const result = await Survey.findByIdAndUpdate(id, dataToSet); if (result) { res.send("response recorded successfully"); From ffec875164e976cb3d64876c3915acb8d0c0a25a Mon Sep 17 00:00:00 2001 From: Santan24 Date: Thu, 24 Jun 2021 21:00:11 +0530 Subject: [PATCH 16/19] Changes in docker --- Dockerfile | 4 +++- app.js | 6 +++++- controllers/auth.js | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index d75b524..8bd46ed 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,4 +5,6 @@ WORKDIR /usr/app COPY package*.json /usr/app/ RUN npm install COPY . /usr/app -CMD ["npm","start"] \ No newline at end of file +RUN npm run build +EXPOSE 3000 +CMD ["npm" "run" "start"] \ No newline at end of file diff --git a/app.js b/app.js index aff2297..5ee4bc8 100644 --- a/app.js +++ b/app.js @@ -10,12 +10,16 @@ const authRoutes = require("./routes/auth"); const surveyRoutes = require("./routes/survey"); const app = express(); + app.use(express.urlencoded({ extended: true })); app.use(express.json()); app.use(cors()); app.use("/survey", surveyRoutes); app.use("/api", authRoutes); - +app.get("/health", function (req, res) { + const message = "Running on port " + process.env.PORT; + return res.send(message); +}); app.listen(process.env.PORT || 3000, () => { console.log("server running on port 3000"); }); diff --git a/controllers/auth.js b/controllers/auth.js index c46e164..fa09913 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -196,6 +196,7 @@ const updateProfile = async (req, res) => { } }; + module.exports = { signup, verifyAccount, From 57aaa7a86de2159ce3ec5510f41e0ed76adfccfe Mon Sep 17 00:00:00 2001 From: Santan24 Date: Thu, 24 Jun 2021 21:05:43 +0530 Subject: [PATCH 17/19] changes in docker file --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 8bd46ed..67d1f94 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM node:alpine ENV PORT 3000 RUN mkdir -p /usr/app -WORKDIR /usr/app +WORKDIR /usr/src/app COPY package*.json /usr/app/ RUN npm install COPY . /usr/app From 9c4bf8de1a484dc2c79b055ba8fb986587969506 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Tue, 29 Jun 2021 12:12:54 +0530 Subject: [PATCH 18/19] docker file changes --- Dockerfile | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 67d1f94..e204d50 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,14 @@ FROM node:alpine + ENV PORT 3000 -RUN mkdir -p /usr/app + +RUN mkdir -p /usr/src/app WORKDIR /usr/src/app -COPY package*.json /usr/app/ + +COPY package*.json /usr/src/app/ RUN npm install -COPY . /usr/app -RUN npm run build -EXPOSE 3000 + + +COPY . /usr/src/app + CMD ["npm" "run" "start"] \ No newline at end of file From f67023eff9494d1257678b4bd7d33c11900ddec4 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Fri, 2 Jul 2021 17:02:24 +0530 Subject: [PATCH 19/19] Docker new image pushed --- .dockerignore file => .dockerignore | 2 +- .vscode/launch.json | 15 ++++++++++ .vscode/tasks.json | 46 +++++++++++++++++++++++++++++ Dockerfile | 13 ++++---- 4 files changed, 68 insertions(+), 8 deletions(-) rename .dockerignore file => .dockerignore (96%) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json diff --git a/.dockerignore file b/.dockerignore similarity index 96% rename from .dockerignore file rename to .dockerignore index 5171c54..de625be 100644 --- a/.dockerignore file +++ b/.dockerignore @@ -1,2 +1,2 @@ -node_modules +node_modules npm-debug.log \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..c519607 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + "configurations": [ + { + "name": "Docker Node.js Launch", + "type": "docker", + "request": "launch", + "preLaunchTask": "docker-run: debug", + "platform": "node", + "node": { + "package": "${workspaceFolder}/node_modules/which-module/package.json", + "localRoot": "${workspaceFolder}/node_modules/which-module" + } + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..4e04ed9 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,46 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "docker-build", + "label": "docker-build", + "platform": "node", + "dockerBuild": { + "dockerfile": "${workspaceFolder}/node_modules/which-module/Dockerfile", + "context": "${workspaceFolder}/node_modules/which-module", + "pull": true + }, + "node": { + "package": "${workspaceFolder}/node_modules/which-module/package.json" + } + }, + { + "type": "docker-run", + "label": "docker-run: release", + "dependsOn": [ + "docker-build" + ], + "platform": "node", + "node": { + "package": "${workspaceFolder}/node_modules/which-module/package.json" + } + }, + { + "type": "docker-run", + "label": "docker-run: debug", + "dependsOn": [ + "docker-build" + ], + "dockerRun": { + "env": { + "DEBUG": "*", + "NODE_ENV": "development" + } + }, + "node": { + "package": "${workspaceFolder}/node_modules/which-module/package.json", + "enableDebugging": true + } + } + ] +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index e204d50..5a38c12 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,13 @@ -FROM node:alpine +FROM node:14.16 -ENV PORT 3000 +WORKDIR /ask-feed -RUN mkdir -p /usr/src/app -WORKDIR /usr/src/app +COPY package.json /ask-feed/package.json -COPY package*.json /usr/src/app/ RUN npm install +COPY . /ask-feed -COPY . /usr/src/app +EXPOSE 3000 -CMD ["npm" "run" "start"] \ No newline at end of file +CMD ["npm","start"] \ No newline at end of file