Skip to content

Instantly share code, notes, and snippets.

View malavsoni's full-sized avatar
🏠
Working from home

Malav Soni malavsoni

🏠
Working from home
View GitHub Profile
async function produceData() {
try {
const result1 = await processResult(); // ProcessResult function returns promise
const result2 = await processResult(result1);
const result3 = await processResult(result2);
return result3;
} catch (error) {
return error;
}
}
const produceData = () => {
getData()
.then(result1 => processResult1(result1))
.then(result2 => processResult2(result2))
.then(result3 => return(result3))
.catch (error) => {
return error
}
}
firstFunction(args, function() {
secondFunction(args, function() {
thirdFunction(args, function() {
// And so on...
})
})
})
// Simple callback function which return results and data
function processData (callback) {
fetchData(function (err, data) {
if (err) {
console.log("An error has occurred. Abort everything!");
return callback(err);
}
data += 1;
callback(data);
});
@malavsoni
malavsoni / index.js
Created May 3, 2020 13:03
Starting point of the application.
const app = require("express")();
const PORT = process.env.PORT || 3000;
require("./startup/routes")(app); // Reggister routes for different end points
require("./startup/database")(); // Module will establish connection to MongoDB
app.listen(PORT, () => {
console.log("Listening to " + PORT);
});
@malavsoni
malavsoni / database.js
Created May 3, 2020 12:53
Startup config for Node JS app.
const mongoose = require("mongoose");
module.exports = function () {
const result = mongoose.connect("mongodb://localhost/app", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
result
.then((response) => {
console.log("Connection Established" + response);
@malavsoni
malavsoni / users.js
Created May 3, 2020 12:49
Route script containing all the valid route for user.
const { User, validate } = require("../models/user");
const express = require("express");
const router = express.Router();
router.get("/", async (req, res) => {
const users = await User.find().sort("firstName");
res.send(users);
});
router.post("/", async (req, res) => {
@malavsoni
malavsoni / User.js
Created May 3, 2020 12:34
File containing the schema of User collection and validation related to it.
const Joi = require("joi");
const mongoose = require("mongoose");
const User = mongoose.model(
"User",
new mongoose.Schema({
firstName: {
type: String,
required: true,
minlength: 5,
@malavsoni
malavsoni / custom-environment-variables.json
Created May 3, 2020 12:30
config file containing all the custom environment variables of the app.
{
"jwtPrivateKey": "app_name_jwtPrivateKey"
}
@malavsoni
malavsoni / auth.js
Created May 3, 2020 12:25
JS file containing logic of JWT Token verification.
const jwt = require('jsonwebtoken');
const config = require('config');
module.exports = function (req, res, next) {
const token = req.header('x-auth-token');
if (!token) return res.status(401).send('Access denied. No token provided.');
try {
const decoded = jwt.verify(token, config.get('jwtPrivateKey'));
req.user = decoded;