This file contains hidden or 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
// user.controller.js | |
const { UserError } = require('./util') | |
const saveUser = async ({ body } = {}) => { | |
if (!body.name) { | |
throw new UserError('name field is missing',400); // operational error | |
} | |
let result1 = await saveUserInDB(body); |
This file contains hidden or 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
// user.route.js | |
const express = require('express'); | |
const router = express.Router(); | |
const { saveUser } = require('./user.controller'); | |
const { Helper } = require('./util'); | |
router.post('/', Helper.requestHandler.call(null,saveUser)); | |
// exporting router object to main file where we have app object | |
module.exports = router; |
This file contains hidden or 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
// user.route.js | |
const express = require('express'); | |
const router = express.Router(); | |
const { saveUser } = require('./user.controller'); | |
router.post('/', saveUser); | |
// exporting router object to main file where we have app object | |
module.exports = router; |
This file contains hidden or 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
class UserError extends Error { | |
constructor(message, statusCode) { | |
super(message) | |
this.name = this.constructor.name; | |
this.statusCode = statusCode; | |
Error.captureStackTrace(this, UserError) | |
} | |
} |
This file contains hidden or 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 saveUser = async (req, res) => { | |
let { body } = req; | |
let result1, result2; | |
if (typeof body !== 'object') { | |
return res.status(400).json({ message: 'data must be an object' }) | |
} | |
try { | |
result1 = await saveUserInDB(body); | |
} |
This file contains hidden or 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 promisifyIt = (fn) => (...args)=>{ | |
return new Promise((resolve,reject)=>{ | |
fn(...args,(err,data)=>{ | |
if(err){ | |
reject(err) | |
} | |
else{ | |
resolve(data) | |
} | |
}) |
This file contains hidden or 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
0x45bef9Be3B4B43e674a2A8A0E9375867A1718CC7 |
This file contains hidden or 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
0xC01af13d073E6eb9c2e4B6CBC789F3f02f9F9CD6 |
This file contains hidden or 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
/** | |
* Fancy ID generator that creates 20-character string identifiers with the following properties: | |
* | |
* 1. They're based on timestamp so that they sort *after* any existing ids. | |
* 2. They contain 72-bits of random data after the timestamp so that IDs won't collide with other clients' IDs. | |
* 3. They sort *lexicographically* (so the timestamp is converted to characters that will sort properly). | |
* 4. They're monotonically increasing. Even if you generate more than one in the same timestamp, the | |
* latter ones will sort after the former ones. We do this by using the previous random bits | |
* but "incrementing" them by 1 (only in the case of a timestamp collision). | |
*/ |
This file contains hidden or 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
Latency Comparison Numbers | |
-------------------------- | |
L1 cache reference 0.5 ns | |
Branch mispredict 5 ns | |
L2 cache reference 7 ns 14x L1 cache | |
Mutex lock/unlock 25 ns | |
Main memory reference 100 ns 20x L2 cache, 200x L1 cache | |
Compress 1K bytes with Zippy 3,000 ns 3 us | |
Send 1K bytes over 1 Gbps network 10,000 ns 10 us | |
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD |
NewerOlder