Like any kind 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.
Move File to a Different Partition or Device in Node.js
We can move file to a different partition or device by specifying the path of the destination of our write stream.
We read the file into a read stream and pipe it to a write stream.
For instance, we can write:
const fs = require('fs');
`
const rs = fs.createReadStream('source/file');
const ws = fs.createWriteStream('destination/file');
`
rs.pipe(ws);
rs.on('end', () => {
fs.unlinkSync('source/file');
});
We call createReadStream
with the source file path to read the file from a stream.
Then we call createWriteStream
to create a write stream with the destination path.
We call pipe
on the read stream with the write stream as the argument to pipe the read stream to the write stream.
This will copy the file from the source to the destination.
Once the copying is done, we call unlinkSync
to delete the file from the source path.
We know when the copying is done when the end
event is emitted.
To make our lives easier, we can use the fs.extra
module’s move
method.
We install it by running:
npm install fs.extra
Then we can use the move
method by specifying the source and destination paths.
For instance, we can write:
const fs = require('fs.extra');
fs.move('foo.txt', 'bar.txt', (err) => {
if (err) {
throw err;
}
console.log("success");
});
We called fs.move
with the source file path as the first argument.
The 2nd argument is the destination path.
The last is the callback which is run when the move operation is done.
Find Deprecation Warnings
We can find deprecation warnings by using the --trace-deprecation
or --throw-deprecation
options when we run our app.
For instance, we can run:
node --trace-deprecation app.js
or:
node --throw-deprecation app.js
— trace-deprecation
logs a stack trace and --throw-deprecation
throw an error.
We should also make sure that we include a callback function in async methods if we get something like:
(node:4346) DeprecationWarning: Calling an asynchronous function without callback is deprecated.
Calling async functions without callbacks is a big deprecated feature.
For instance, we should write:
const fs = require('fs');
//...
`
fs.writeFile('foo.txt', data, 'utf8', (error) => {
// ...
});
The 4th argument is the callback function.
Node.js exec does not work with the “cd ” Shell Command
Each command is run in its own shell with exec
, so using cd
only affect the shell process that cd
is run on.
If we want to change the current working directory, we’ve to set the cwd
option.
For instance, we can write:
exec('git status', {
cwd: '/repo/folder'
}, (error, stdout, stderr) => {
if (error) {
console.error(error);
return;
}
console.log(stdout);
console.error(stderr);
});
We switch to the /repo/folder
directory by setting the cwd
option in the object we passed in as the 2nd argument.
Then we can run git status
on /repo/folder
instead of whatever directory the app is running in.
Schema within a Schema with Mongoose
We can define a schema within a schema with Mongoose.
This way, we can store relational data with it.
For instance, we can write:
const UserSchema = new Schema({
name: String,
tasks: [{
type: Schema.ObjectId,
ref: 'Task'
}]
});
`
const TaskSchema = new Schema({
name: String,
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
We have 2 schemas that reference each other.
We created a one to many relationships between UserSchema
and TaskSchema
with the tasks
array.
The ref
specifies the schema that we want to access.
An array indicates that it’s one to many.
Likewise, in TaskSchema
, we define the user
field which creates a belongs to relationship with User
.
We specify an object with the 'User’
as the value of ref
.
Then when we query a user, we can get all its tasks by using the populate
method:
User.find({}).populate('tasks').run((err, users) => {
//...
});
We pass in the child field we want to access, which is tasks
.
Conclusion
We can define nested schemas with Mongoose to create relationships.
There are several ways to move files.
We can run node
with some options to find deprecation warnings.
To change the current working directory with exec
, we can set the cwd
property.