This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const produceData = () => { | |
getData() | |
.then(result1 => processResult1(result1)) | |
.then(result2 => processResult2(result2)) | |
.then(result3 => return(result3)) | |
.catch (error) => { | |
return error | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
firstFunction(args, function() { | |
secondFunction(args, function() { | |
thirdFunction(args, function() { | |
// And so on... | |
}) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Joi = require("joi"); | |
const mongoose = require("mongoose"); | |
const User = mongoose.model( | |
"User", | |
new mongoose.Schema({ | |
firstName: { | |
type: String, | |
required: true, | |
minlength: 5, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"jwtPrivateKey": "app_name_jwtPrivateKey" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
NewerOlder