ind of apps, there are difficult issues to solve when we write Node apps.
In this article, we’ll look at some solutions to common problems when writing Node apps.
Storing passwords with Node.js and MongoDB
We can store passwords in a MongoDB document with the bcrypt
library.
For instance. we can write:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcrypt = require('bcrypt');
const SALT_WORK_FACTOR = 10;
const UserSchema = new Schema({
username: {
type: String,
required: true,
index: {
unique: true
}
},
password: { type: String, required: true }
});
module.exports = mongoose.model('User', UserSchema);
We create the UserSchema
with the Schema
constructor.
It has the username
and password
string fields.
Then we can use the pre
method to hash and salt the password before it’s saved.
To do that, we write:
UserSchema.pre('save', function(next) {
const user = this;
if (!user.isModified('password')){
return next();
}
bcrypt.genSalt(SALT_WORK_FACTOR, (err, salt) => {
if (err) {
return next(err);
}
bcrypt.hash(user.password, salt, (err, hash) => {
if (err) {
return next(err);
}
user.password = hash;
next();
});
});
});
We override the plain text password with the hash and salted one/
To do that, we used the genSalt
method to create the salt.
Then we create the hash from the password.
We passed our generated salt
into the hashmethod.
Then we set the password
property to the hash
.
And finally, we call next
to continue with saving the user data.
Fix Protocol “https:” not supported. Expected “http:” Error
We can fix this error, which occurs when we try to make HTTPS requests with http
.
We’ve to use https.get
instead of http.get
to make a request to a URL that starts with https
.
Mongoose Connect Error Callback
We can pick up the error in the callback we pass into connect
.
For example, we can write:
mongoose.connect('mongodb://localhost/db', (err) => {
if (err) throw err;
});
err
has the error object that’s set when there’s an error.
Run Some Code and Then Go Into Node REPL
We can use the repl
module to start the REPL.
For instance, we can write:
const repl = require("repl");
const r = repl.start("node> ");
We call start
the REPL with the command prompt of our choice.
Get the Node.js Version at Runtime
We can get the Node version at runtime with the process.version
property.
To get more version information, we can use the process.versions
property.
Then we get something like:
{
node: '12.16.3',
v8: '7.8.279.23-node.35',
uv: '1.34.2',
zlib: '1.2.11',
brotli: '1.0.7',
ares: '1.16.0',
modules: '72',
nghttp2: '1.40.0',
napi: '5',
llhttp: '2.0.4',
http_parser: '2.9.3',
openssl: '1.1.1g',
cldr: '36.0',
icu: '65.1',
tz: '2019c',
unicode: '12.1'
}
Unzip (Decompress) a NodeJS Request’s Module gzip Response Body
We can decompress a gzipped file by using the zlib
library.
For instance, we can write:
const http = require("http"),
const zlib = require("zlib");
const getGzipped = (url, callback) => {
const buffer = [];
http.get(url, (res) => {
const gunzip = zlib.createGunzip();
res.pipe(gunzip);
gunzip.on('data', (data) => {
buffer.push(data.toString())
})
.on("end", () => {
callback(null, buffer.join(""));
})
.on("error", (e) => {
callback(e);
})
})
.on('error', (e) => {
callback(e);
});
}
getGzipped(url, (err, data) => {
console.log(data);
});
We created the getGzipped
function by making the GET request to get a zipped file with the http.get
method.
Then call createGunzip
to create the gunzip
object that we can pipe the res
stream object to.
Then we can listen to the data
event to get the data.
We call push
on the buffer
object to gather the data.
Then when the end
event is emitted, we call the callback
to send the data to the callback.
When an error
event is emitted from the gunzip
or http.get
methods, we’ll call the callback
to send the error.
Then we can use the function to get the error or the unzipped file’s data.
Conclusion
We can get the version with the process
object.
To unzip files obtained from the http.get
method, we can unzip it with zlib
.
We’ve to hash and salt the password manually if we want to store it securely.
We can use the repl
module to access the REPL.