react-express-mongodb: lint sources
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 <jeremie.drouet@gmail.com>
This commit is contained in:
parent
be8875c9b6
commit
be7f09b6ba
1
react-express-mongodb/.gitignore
vendored
1
react-express-mongodb/.gitignore
vendored
@ -2,3 +2,4 @@ frontend/node_modules/
|
|||||||
server/node_modules/
|
server/node_modules/
|
||||||
.idea/
|
.idea/
|
||||||
data
|
data
|
||||||
|
*.log
|
||||||
|
17
react-express-mongodb/backend/config/config.js
vendored
17
react-express-mongodb/backend/config/config.js
vendored
@ -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')
|
Object.keys(envConfig).forEach((key) => {
|
||||||
let envConfig = config[env];
|
process.env[key] = envConfig[key];
|
||||||
console.log(envConfig);
|
});
|
||||||
|
|
||||||
Object.keys(envConfig).forEach((key) => {
|
|
||||||
process.env[key] = envConfig[key]
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
120
react-express-mongodb/backend/config/messages.js
vendored
120
react-express-mongodb/backend/config/messages.js
vendored
@ -1,62 +1,62 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
AUTHENTICATION_FAILED: {
|
AUTHENTICATION_FAILED: {
|
||||||
code: 400,
|
code: 400,
|
||||||
message: 'Authentication failed. Please login with valid credentials.',
|
message: "Authentication failed. Please login with valid credentials.",
|
||||||
success: false,
|
success: false,
|
||||||
},
|
},
|
||||||
SUCCESSFUL_LOGIN: {
|
SUCCESSFUL_LOGIN: {
|
||||||
code: 200,
|
code: 200,
|
||||||
message: 'Successfully logged in',
|
message: "Successfully logged in",
|
||||||
success: true,
|
success: true,
|
||||||
},
|
},
|
||||||
INTERNAL_SERVER_ERROR: {
|
INTERNAL_SERVER_ERROR: {
|
||||||
code: 500,
|
code: 500,
|
||||||
message: 'Something unexpected happened',
|
message: "Something unexpected happened",
|
||||||
success: false,
|
success: false,
|
||||||
},
|
},
|
||||||
UNAUTHORIZED: {
|
UNAUTHORIZED: {
|
||||||
code: 401,
|
code: 401,
|
||||||
message: 'You session is expired. Please login again',
|
message: "You session is expired. Please login again",
|
||||||
success: false,
|
success: false,
|
||||||
},
|
},
|
||||||
SUCCESSFUL_DELETE: {
|
SUCCESSFUL_DELETE: {
|
||||||
code: 200,
|
code: 200,
|
||||||
message: 'Successfully deleted',
|
message: "Successfully deleted",
|
||||||
success: true,
|
success: true,
|
||||||
},
|
},
|
||||||
SUCCESSFUL_UPDATE: {
|
SUCCESSFUL_UPDATE: {
|
||||||
code: 200,
|
code: 200,
|
||||||
message: 'Updated successfully',
|
message: "Updated successfully",
|
||||||
success: true,
|
success: true,
|
||||||
},
|
},
|
||||||
SUCCESSFUL: {
|
SUCCESSFUL: {
|
||||||
code: 200,
|
code: 200,
|
||||||
success: true,
|
success: true,
|
||||||
message: 'Successfully completed',
|
message: "Successfully completed",
|
||||||
},
|
},
|
||||||
NOT_FOUND: {
|
NOT_FOUND: {
|
||||||
code: 404,
|
code: 404,
|
||||||
success: true,
|
success: true,
|
||||||
message: 'Requested API not found',
|
message: "Requested API not found",
|
||||||
},
|
},
|
||||||
ALREADY_EXIST: {
|
ALREADY_EXIST: {
|
||||||
code: 200,
|
code: 200,
|
||||||
success: true,
|
success: true,
|
||||||
message: 'Already exists',
|
message: "Already exists",
|
||||||
},
|
},
|
||||||
FORBIDDEN: {
|
FORBIDDEN: {
|
||||||
code: 403,
|
code: 403,
|
||||||
message: 'You are not authorized to complete this action',
|
message: "You are not authorized to complete this action",
|
||||||
success: false,
|
success: false,
|
||||||
},
|
},
|
||||||
BAD_REQUEST: {
|
BAD_REQUEST: {
|
||||||
code: 400,
|
code: 400,
|
||||||
message: 'Bad request. Please try again with valid parameters',
|
message: "Bad request. Please try again with valid parameters",
|
||||||
success: false,
|
success: false,
|
||||||
},
|
},
|
||||||
IN_COMPLETE_REQUEST: {
|
IN_COMPLETE_REQUEST: {
|
||||||
code: 422,
|
code: 422,
|
||||||
message: 'Required parameter missing',
|
message: "Required parameter missing",
|
||||||
success: false,
|
success: false,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
48
react-express-mongodb/backend/db/index.js
vendored
48
react-express-mongodb/backend/db/index.js
vendored
@ -1,30 +1,32 @@
|
|||||||
/**
|
/**
|
||||||
* Created by Syed Afzal
|
* Created by Syed Afzal
|
||||||
*/
|
*/
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require("mongoose");
|
||||||
const {log} = require('../utils/helpers/logger');
|
|
||||||
|
|
||||||
exports.connect = (app) => {
|
exports.connect = (app) => {
|
||||||
const options = {
|
const options = {
|
||||||
useNewUrlParser: true,
|
useNewUrlParser: true,
|
||||||
autoIndex: false, // Don't build indexes
|
autoIndex: false, // Don't build indexes
|
||||||
reconnectTries: 30, // Retry up to 30 times
|
reconnectTries: 30, // Retry up to 30 times
|
||||||
reconnectInterval: 500, // Reconnect every 500ms
|
reconnectInterval: 500, // Reconnect every 500ms
|
||||||
poolSize: 10, // Maintain up to 10 socket connections
|
poolSize: 10, // Maintain up to 10 socket connections
|
||||||
// If not connected, return errors immediately rather than waiting for reconnect
|
// If not connected, return errors immediately rather than waiting for reconnect
|
||||||
bufferMaxEntries: 0
|
bufferMaxEntries: 0,
|
||||||
}
|
};
|
||||||
|
|
||||||
const connectWithRetry = () => {
|
const connectWithRetry = () => {
|
||||||
mongoose.Promise = global.Promise;
|
mongoose.Promise = global.Promise;
|
||||||
console.log('MongoDB connection with retry')
|
console.log("MongoDB connection with retry");
|
||||||
mongoose.connect(process.env.MONGODB_URI, options).then(()=>{
|
mongoose
|
||||||
console.log('MongoDB is connected');
|
.connect(process.env.MONGODB_URI, options)
|
||||||
app.emit('ready');
|
.then(() => {
|
||||||
}).catch(err=>{
|
console.log("MongoDB is connected");
|
||||||
console.log('MongoDB connection unsuccessful, retry after 2 seconds.')
|
app.emit("ready");
|
||||||
setTimeout(connectWithRetry, 2000)
|
})
|
||||||
})
|
.catch((err) => {
|
||||||
}
|
console.log("MongoDB connection unsuccessful, retry after 2 seconds.");
|
||||||
connectWithRetry();
|
setTimeout(connectWithRetry, 2000);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
connectWithRetry();
|
||||||
};
|
};
|
||||||
|
0
react-express-mongodb/backend/logs/.gitkeep
Normal file
0
react-express-mongodb/backend/logs/.gitkeep
Normal file
Binary file not shown.
@ -3,7 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
|
|
||||||
var Todo = mongoose.model('Todo', {
|
const Todo = mongoose.model('Todo', {
|
||||||
text : {
|
text : {
|
||||||
type: String,
|
type: String,
|
||||||
trim: true,
|
trim: true,
|
6
react-express-mongodb/backend/package-lock.json
generated
6
react-express-mongodb/backend/package-lock.json
generated
@ -1100,6 +1100,12 @@
|
|||||||
"integrity": "sha1-6SQ0v6XqjBn0HN/UAddBo8gZ2Jc=",
|
"integrity": "sha1-6SQ0v6XqjBn0HN/UAddBo8gZ2Jc=",
|
||||||
"dev": true
|
"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": {
|
"process-nextick-args": {
|
||||||
"version": "2.0.1",
|
"version": "2.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node server.js",
|
"start": "node server.js",
|
||||||
|
"format": "prettier --write {**/,}*.js",
|
||||||
|
"lint": "prettier --check {**/,}*.js",
|
||||||
"dev": "nodemon server.js"
|
"dev": "nodemon server.js"
|
||||||
},
|
},
|
||||||
"author": "Syed Afzal",
|
"author": "Syed Afzal",
|
||||||
@ -22,6 +24,7 @@
|
|||||||
"validator": "^10.1.0"
|
"validator": "^10.1.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"nodemon": "^2.0.3"
|
"nodemon": "^2.0.3",
|
||||||
|
"prettier": "^2.0.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
60
react-express-mongodb/backend/routes/index.js
vendored
60
react-express-mongodb/backend/routes/index.js
vendored
@ -1,38 +1,38 @@
|
|||||||
const express = require('express');
|
const express = require("express");
|
||||||
const serverResponses = require('../utils/helpers/server.responses');
|
const serverResponses = require("../utils/helpers/responses");
|
||||||
const messages = require('../config/messages');
|
const messages = require("../config/messages");
|
||||||
var {Todo} = require('../models/todos/todo.model');
|
const { Todo } = require("../models/todos/todo");
|
||||||
|
|
||||||
const routes = (app) => {
|
const routes = (app) => {
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.post('/todos', (req,res)=>{
|
router.post("/todos", (req, res) => {
|
||||||
var todo = new Todo({
|
const todo = new Todo({
|
||||||
text: req.body.text
|
text: req.body.text,
|
||||||
});
|
|
||||||
|
|
||||||
todo.save()
|
|
||||||
.then((result)=>{
|
|
||||||
serverResponses.sendSuccess(res,messages.SUCCESSFUL, result);
|
|
||||||
})
|
|
||||||
.catch((e) => {
|
|
||||||
serverResponses.sendError(res,messages.BAD_REQUEST,e)
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/', (req,res) => {
|
todo
|
||||||
Todo.find({}, {__v:0})
|
.save()
|
||||||
.then((todos)=>{
|
.then((result) => {
|
||||||
serverResponses.sendSuccess(res,messages.SUCCESSFUL, todos);
|
serverResponses.sendSuccess(res, messages.SUCCESSFUL, result);
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
serverResponses.sendError(res, messages.BAD_REQUEST, e);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
})
|
router.get("/", (req, res) => {
|
||||||
.catch((e) => {
|
Todo.find({}, { __v: 0 })
|
||||||
serverResponses.sendError(res,messages.BAD_REQUEST,e)
|
.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
|
//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
|
//differentiate b/w each module you can use this technique
|
||||||
app.use('/api', router);
|
app.use("/api", router);
|
||||||
};
|
};
|
||||||
module.exports = routes;
|
module.exports = routes;
|
||||||
|
30
react-express-mongodb/backend/server.js
vendored
30
react-express-mongodb/backend/server.js
vendored
@ -1,16 +1,14 @@
|
|||||||
/**
|
/**
|
||||||
* Created by Syed Afzal
|
* Created by Syed Afzal
|
||||||
*/
|
*/
|
||||||
require('./config/config');
|
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 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();
|
const app = express();
|
||||||
|
|
||||||
@ -21,15 +19,15 @@ app.use(cors());
|
|||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
app.use(bodyParser.urlencoded({ extended: false }));
|
app.use(bodyParser.urlencoded({ extended: false }));
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
app.use(express.static(path.join(__dirname, 'public')));
|
app.use(express.static(path.join(__dirname, "public")));
|
||||||
|
|
||||||
// adding routes
|
// adding routes
|
||||||
require('./routes')(app);
|
require("./routes")(app);
|
||||||
|
|
||||||
app.on('ready', () => {
|
app.on("ready", () => {
|
||||||
app.listen(3000, () => {
|
app.listen(3000, () => {
|
||||||
console.log("Server is up on port", 3000)
|
console.log("Server is up on port", 3000);
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
module.exports = app;
|
module.exports = app;
|
||||||
|
Loading…
Reference in New Issue
Block a user