Categories
JavaScript Answers

How to get file extension with Node.js?

Sometimes, we want to get file extension with Node.js.

In this article, we’ll look at how to get file extension with Node.js.

How to get file extension with Node.js?

To get file extension with Node.js, we can use the path.extname method.

For instance, we write

const path = require('path')

const ext = path.extname('index.html')

to call path.extname with 'index.html' to return the extension of index.html.

Conclusion

To get file extension with Node.js, we can use the path.extname method.

Categories
JavaScript Answers

How to add user authentication for Node.js?

Sometimes, we want to add user authentication for Node.js.

In this article, we’ll look at how to add user authentication for Node.js.

How to add user authentication for Node.js?

To add user authentication for Node.js, we can use Passport.

For instance, we write

passport.use(new LocalStrategy(
  (username, password, done) => {
    User.findOne({
      username,
      password
    }, (err, user) => {
      done(err, user);
    });
  }
));

app.post('/login',
  passport.authenticate('local', {
    failureRedirect: '/login'
  }),
  (req, res) => {
    res.redirect('/');
  });

to add Passport to the Express app we have.

We call passport.use with a LocalStrategy instance to let us authenticate users by looking them up from our own MongoDB database.

We use User.findOne to find the user.

Then we add the /login route that uses the middleware returned by passport.authenticate for authentication before we redirect to /.

Conclusion

To add user authentication for Node.js, we can use Passport.

Categories
JavaScript Answers

How to stop Mongoose from creating _id property for sub-document array items?

Sometimes, we want to stop Mongoose from creating _id property for sub-document array items.

In this article, we’ll look at how to stop Mongoose from creating _id property for sub-document array items.

How to stop Mongoose from creating _id property for sub-document array items?

To stop Mongoose from creating _id property for sub-document array items, we can call mongoose.Schema with _id set to false.

For instance, we write

const mongoose = require("mongoose");

const subSchema = mongoose.Schema({}, {
  _id: false
});

const schema = mongoose.Schema({
  subSchemaCollection: [subSchema]
});

const model = mongoose.model('tablename', schema);

to create subSchema by calling mongoose.Schema with an object that has _id set to false.

Then we put the subSchema in the parent schema in the array we set as the value of subSchemaCollection.

Conclusion

To stop Mongoose from creating _id property for sub-document array items, we can call mongoose.Schema with _id set to false.

Categories
JavaScript Answers

How to call a Python function from Node.js?

Sometimes, we want to call a Python function from Node.js.

In this article, we’ll look at how to call a Python function from Node.js.

How to call a Python function from Node.js?

To call a Python function from Node.js, we can call the spawn method.

For instance, we write

const {
  spawn
} = require("child_process");
const pythonProcess = spawn('python', ["path/to/script.py", arg1, arg2]);

to call the spawn method available in the child_process module.

We run python with the path to the script, arg1 and arg2 as the command line arguments.

The function we want to call should be in script.py.

Conclusion

To call a Python function from Node.js, we can call the spawn method.

Categories
JavaScript Answers

How to make a Node.js application run permanently?

Sometimes, we want to make a Node.js application run permanently.

In this article, we’ll look at how to make a Node.js application run permanently.

How to make a Node.js application run permanently?

To make a Node.js application run permanently, we can use the forever package.

We install it globally by running

sudo npm install -g forever

Then we can start our app with it with

forever server.js

We can also run it as a service with

forever start server.js

We can restricts restarts to 5 times when it crashes or stop unexpectedly with

forever -m5 server.js

We can stop a process with

forever stop 0

where 0 is the process ID.

And we can restart a process with

forever restart 0

where 0 is the process ID.

Conclusion

To make a Node.js application run permanently, we can use the forever package.