From be7f09b6ba812faa4dfa6cab79cb163dcc6ea2c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Drouet?= Date: Tue, 12 May 2020 15:16:03 +0200 Subject: [PATCH] react-express-mongodb: lint sources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit to fix some syntax issues in the existing code, the commit put in place prettier and format the code Signed-off-by: Jérémie Drouet --- react-express-mongodb/.gitignore | 1 + .../backend/config/config.js | 17 ++- .../backend/config/messages.js | 120 +++++++++--------- react-express-mongodb/backend/db/index.js | 48 +++---- react-express-mongodb/backend/logs/.gitkeep | 0 .../backend/logs/project.log | Bin 5164 -> 0 bytes .../models/todos/{todo.model.js => todo.js} | 2 +- .../backend/package-lock.json | 6 + react-express-mongodb/backend/package.json | 5 +- react-express-mongodb/backend/routes/index.js | 60 ++++----- react-express-mongodb/backend/server.js | 30 ++--- .../{server.responses.js => responses.js} | 0 12 files changed, 149 insertions(+), 140 deletions(-) create mode 100644 react-express-mongodb/backend/logs/.gitkeep delete mode 100644 react-express-mongodb/backend/logs/project.log rename react-express-mongodb/backend/models/todos/{todo.model.js => todo.js} (83%) rename react-express-mongodb/backend/utils/helpers/{server.responses.js => responses.js} (100%) diff --git a/react-express-mongodb/.gitignore b/react-express-mongodb/.gitignore index 29a0139..83a01ad 100644 --- a/react-express-mongodb/.gitignore +++ b/react-express-mongodb/.gitignore @@ -2,3 +2,4 @@ frontend/node_modules/ server/node_modules/ .idea/ data +*.log diff --git a/react-express-mongodb/backend/config/config.js b/react-express-mongodb/backend/config/config.js index 25ef5d0..4a3b891 100644 --- a/react-express-mongodb/backend/config/config.js +++ b/react-express-mongodb/backend/config/config.js @@ -1,12 +1,11 @@ -var env = process.env.NODE_ENV || 'development'; +const env = process.env.NODE_ENV || "development"; -if(env === 'development' || env === 'test'){ +if (env === "development" || env === "test") { + const config = require("./config.json"); + const envConfig = config[env]; + console.log(envConfig); - const config = require('./config.json') - let envConfig = config[env]; - console.log(envConfig); - - Object.keys(envConfig).forEach((key) => { - process.env[key] = envConfig[key] - }) + Object.keys(envConfig).forEach((key) => { + process.env[key] = envConfig[key]; + }); } diff --git a/react-express-mongodb/backend/config/messages.js b/react-express-mongodb/backend/config/messages.js index 1958fdd..f02f69c 100644 --- a/react-express-mongodb/backend/config/messages.js +++ b/react-express-mongodb/backend/config/messages.js @@ -1,62 +1,62 @@ module.exports = { - AUTHENTICATION_FAILED: { - code: 400, - message: 'Authentication failed. Please login with valid credentials.', - success: false, - }, - SUCCESSFUL_LOGIN: { - code: 200, - message: 'Successfully logged in', - success: true, - }, - INTERNAL_SERVER_ERROR: { - code: 500, - message: 'Something unexpected happened', - success: false, - }, - UNAUTHORIZED: { - code: 401, - message: 'You session is expired. Please login again', - success: false, - }, - SUCCESSFUL_DELETE: { - code: 200, - message: 'Successfully deleted', - success: true, - }, - SUCCESSFUL_UPDATE: { - code: 200, - message: 'Updated successfully', - success: true, - }, - SUCCESSFUL: { - code: 200, - success: true, - message: 'Successfully completed', - }, - NOT_FOUND: { - code: 404, - success: true, - message: 'Requested API not found', - }, - ALREADY_EXIST: { - code: 200, - success: true, - message: 'Already exists', - }, - FORBIDDEN: { - code: 403, - message: 'You are not authorized to complete this action', - success: false, - }, - BAD_REQUEST: { - code: 400, - message: 'Bad request. Please try again with valid parameters', - success: false, - }, - IN_COMPLETE_REQUEST: { - code: 422, - message: 'Required parameter missing', - success: false, - }, + AUTHENTICATION_FAILED: { + code: 400, + message: "Authentication failed. Please login with valid credentials.", + success: false, + }, + SUCCESSFUL_LOGIN: { + code: 200, + message: "Successfully logged in", + success: true, + }, + INTERNAL_SERVER_ERROR: { + code: 500, + message: "Something unexpected happened", + success: false, + }, + UNAUTHORIZED: { + code: 401, + message: "You session is expired. Please login again", + success: false, + }, + SUCCESSFUL_DELETE: { + code: 200, + message: "Successfully deleted", + success: true, + }, + SUCCESSFUL_UPDATE: { + code: 200, + message: "Updated successfully", + success: true, + }, + SUCCESSFUL: { + code: 200, + success: true, + message: "Successfully completed", + }, + NOT_FOUND: { + code: 404, + success: true, + message: "Requested API not found", + }, + ALREADY_EXIST: { + code: 200, + success: true, + message: "Already exists", + }, + FORBIDDEN: { + code: 403, + message: "You are not authorized to complete this action", + success: false, + }, + BAD_REQUEST: { + code: 400, + message: "Bad request. Please try again with valid parameters", + success: false, + }, + IN_COMPLETE_REQUEST: { + code: 422, + message: "Required parameter missing", + success: false, + }, }; diff --git a/react-express-mongodb/backend/db/index.js b/react-express-mongodb/backend/db/index.js index 96d78fb..6598305 100644 --- a/react-express-mongodb/backend/db/index.js +++ b/react-express-mongodb/backend/db/index.js @@ -1,30 +1,32 @@ /** * Created by Syed Afzal */ -const mongoose = require('mongoose'); -const {log} = require('../utils/helpers/logger'); +const mongoose = require("mongoose"); exports.connect = (app) => { - const options = { - useNewUrlParser: true, - autoIndex: false, // Don't build indexes - reconnectTries: 30, // Retry up to 30 times - reconnectInterval: 500, // Reconnect every 500ms - poolSize: 10, // Maintain up to 10 socket connections - // If not connected, return errors immediately rather than waiting for reconnect - bufferMaxEntries: 0 - } + const options = { + useNewUrlParser: true, + autoIndex: false, // Don't build indexes + reconnectTries: 30, // Retry up to 30 times + reconnectInterval: 500, // Reconnect every 500ms + poolSize: 10, // Maintain up to 10 socket connections + // If not connected, return errors immediately rather than waiting for reconnect + bufferMaxEntries: 0, + }; - const connectWithRetry = () => { - mongoose.Promise = global.Promise; - console.log('MongoDB connection with retry') - mongoose.connect(process.env.MONGODB_URI, options).then(()=>{ - console.log('MongoDB is connected'); - app.emit('ready'); - }).catch(err=>{ - console.log('MongoDB connection unsuccessful, retry after 2 seconds.') - setTimeout(connectWithRetry, 2000) - }) - } - connectWithRetry(); + const connectWithRetry = () => { + mongoose.Promise = global.Promise; + console.log("MongoDB connection with retry"); + mongoose + .connect(process.env.MONGODB_URI, options) + .then(() => { + console.log("MongoDB is connected"); + app.emit("ready"); + }) + .catch((err) => { + console.log("MongoDB connection unsuccessful, retry after 2 seconds."); + setTimeout(connectWithRetry, 2000); + }); + }; + connectWithRetry(); }; diff --git a/react-express-mongodb/backend/logs/.gitkeep b/react-express-mongodb/backend/logs/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/react-express-mongodb/backend/logs/project.log b/react-express-mongodb/backend/logs/project.log deleted file mode 100644 index f7809021b2905f6037b28fac157c779f4bc10663..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5164 zcmcJTO>Wyj428SyDdqx#NzRN$;;umuAd94%Tp+cLc9B{E;`^g-WJ7XVN1bZeK*%DC z59B;P{zQoThPa{Daj(*AjPCyN?yGYT!{PgId*iml;jrHx_d7Qn+|T^S-8bEAUWV=S z%a7shsFH}&=D2@5-rhXnxBD#bpq@3f=1{NH8$p>GZXG4{0bdz(e29-ypMg<1nF4zy zF#~d|!K+^>&eO0PzP!F(NPfyYQMT$rDX*-~B`PZwYV>=}FH<9x3H2{jQU4!a zz{B`ev7RkI=^wk=IIj>xXB}f?L!#FpS{mbJ`#4w8`;5KDfc8=rUl}c!(2}E8BifJ^ zEh$B%La!`mnjEu=D27?l3|ppkjzuNQYCsdo114f>S-e}Ob@|=6&=ZT9f`)hV0InE* ze^jqT$D{#Yp}Ur2>vO$@2s>D)02;=8t71_hXqun}*ug~86Ac@fg%=PTR1kb76>_3s zku!`9ni|qt_1>mJ^1Gqr3Mb;R4Gq)6 zS((NfTO@0sX{HHUr3Q;0GSQ+*Y&_0W^j-^}deD$2c6tkEfd&y~F)$h{Y{zVsrv>II=piwG}YzX4loolhs4;N8kE)aBT`%M!r#b@g_RZKUBuTXRNU z%=(FzyCtf(uJ_I~Mypg@<2C!+B@J86nv?!&rWk)HIlI^*=0Z&;pQ?9s7KC6c74jid xyA94N#5_;URKVI`=LHxcSBR0b7~5b{uIs(?yWw8Irdl_%W?F}TMb%xDzW}h}&Q|~c diff --git a/react-express-mongodb/backend/models/todos/todo.model.js b/react-express-mongodb/backend/models/todos/todo.js similarity index 83% rename from react-express-mongodb/backend/models/todos/todo.model.js rename to react-express-mongodb/backend/models/todos/todo.js index b8bdf11..a31ab43 100644 --- a/react-express-mongodb/backend/models/todos/todo.model.js +++ b/react-express-mongodb/backend/models/todos/todo.js @@ -3,7 +3,7 @@ */ const mongoose = require('mongoose'); -var Todo = mongoose.model('Todo', { +const Todo = mongoose.model('Todo', { text : { type: String, trim: true, diff --git a/react-express-mongodb/backend/package-lock.json b/react-express-mongodb/backend/package-lock.json index 9d355be..4d3509a 100644 --- a/react-express-mongodb/backend/package-lock.json +++ b/react-express-mongodb/backend/package-lock.json @@ -1100,6 +1100,12 @@ "integrity": "sha1-6SQ0v6XqjBn0HN/UAddBo8gZ2Jc=", "dev": true }, + "prettier": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.0.5.tgz", + "integrity": "sha512-7PtVymN48hGcO4fGjybyBSIWDsLU4H4XlvOHfq91pz9kkGlonzwTfYkaIEwiRg/dAJF9YlbsduBAgtYLi+8cFg==", + "dev": true + }, "process-nextick-args": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", diff --git a/react-express-mongodb/backend/package.json b/react-express-mongodb/backend/package.json index 8b31b4f..000333f 100644 --- a/react-express-mongodb/backend/package.json +++ b/react-express-mongodb/backend/package.json @@ -5,6 +5,8 @@ "main": "index.js", "scripts": { "start": "node server.js", + "format": "prettier --write {**/,}*.js", + "lint": "prettier --check {**/,}*.js", "dev": "nodemon server.js" }, "author": "Syed Afzal", @@ -22,6 +24,7 @@ "validator": "^10.1.0" }, "devDependencies": { - "nodemon": "^2.0.3" + "nodemon": "^2.0.3", + "prettier": "^2.0.5" } } diff --git a/react-express-mongodb/backend/routes/index.js b/react-express-mongodb/backend/routes/index.js index 274ed03..05b311b 100644 --- a/react-express-mongodb/backend/routes/index.js +++ b/react-express-mongodb/backend/routes/index.js @@ -1,38 +1,38 @@ -const express = require('express'); -const serverResponses = require('../utils/helpers/server.responses'); -const messages = require('../config/messages'); -var {Todo} = require('../models/todos/todo.model'); +const express = require("express"); +const serverResponses = require("../utils/helpers/responses"); +const messages = require("../config/messages"); +const { Todo } = require("../models/todos/todo"); const routes = (app) => { - const router = express.Router(); + const router = express.Router(); - router.post('/todos', (req,res)=>{ - var todo = new Todo({ - text: req.body.text - }); - - todo.save() - .then((result)=>{ - serverResponses.sendSuccess(res,messages.SUCCESSFUL, result); - }) - .catch((e) => { - serverResponses.sendError(res,messages.BAD_REQUEST,e) - }) + router.post("/todos", (req, res) => { + const todo = new Todo({ + text: req.body.text, }); - router.get('/', (req,res) => { - Todo.find({}, {__v:0}) - .then((todos)=>{ - serverResponses.sendSuccess(res,messages.SUCCESSFUL, todos); + todo + .save() + .then((result) => { + serverResponses.sendSuccess(res, messages.SUCCESSFUL, result); + }) + .catch((e) => { + serverResponses.sendError(res, messages.BAD_REQUEST, e); + }); + }); - }) - .catch((e) => { - serverResponses.sendError(res,messages.BAD_REQUEST,e) - }) - }); + router.get("/", (req, res) => { + Todo.find({}, { __v: 0 }) + .then((todos) => { + serverResponses.sendSuccess(res, messages.SUCCESSFUL, todos); + }) + .catch((e) => { + serverResponses.sendError(res, messages.BAD_REQUEST, e); + }); + }); - //it's a prefix before api it is useful when you have many modules and you want to - //differentiate b/w each module you can use this technique - app.use('/api', router); + //it's a prefix before api it is useful when you have many modules and you want to + //differentiate b/w each module you can use this technique + app.use("/api", router); }; -module.exports = routes; \ No newline at end of file +module.exports = routes; diff --git a/react-express-mongodb/backend/server.js b/react-express-mongodb/backend/server.js index fd22b6f..6aa055c 100644 --- a/react-express-mongodb/backend/server.js +++ b/react-express-mongodb/backend/server.js @@ -1,16 +1,14 @@ /** * Created by Syed Afzal */ -require('./config/config'); - -const express = require('express'); -const path = require('path'); -const cookieParser = require('cookie-parser'); -const bodyParser = require('body-parser'); -const cors = require('cors'); -const db = require('./db'); - +require("./config/config"); +const express = require("express"); +const path = require("path"); +const cookieParser = require("cookie-parser"); +const bodyParser = require("body-parser"); +const cors = require("cors"); +const db = require("./db"); const app = express(); @@ -21,15 +19,15 @@ app.use(cors()); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); -app.use(express.static(path.join(__dirname, 'public'))); +app.use(express.static(path.join(__dirname, "public"))); // adding routes -require('./routes')(app); +require("./routes")(app); -app.on('ready', () => { - app.listen(3000, () => { - console.log("Server is up on port", 3000) - }); -}) +app.on("ready", () => { + app.listen(3000, () => { + console.log("Server is up on port", 3000); + }); +}); module.exports = app; diff --git a/react-express-mongodb/backend/utils/helpers/server.responses.js b/react-express-mongodb/backend/utils/helpers/responses.js similarity index 100% rename from react-express-mongodb/backend/utils/helpers/server.responses.js rename to react-express-mongodb/backend/utils/helpers/responses.js