Categories
JavaScript Answers

How to get the path from the request with Node.js?

Sometimes, we want to get the path from the request with Node.js.

In this article, we’ll look at how to get the path from the request with Node.js.

How to get the path from the request with Node.js?

To get the path from the request with Node.js, we can use the req.url property.

For instance, we write

const http = require('http');
const url = require('url');

http
  .createServer((req, res) => {
    res.send(req.url)
  })
  .listen(8080, '0.0.0.0');

to call createServer with a callback that gets the request path from req.url.

And we call res.send to send that has the response.

Conclusion

To get the path from the request with Node.js, we can use the req.url property.

Categories
JavaScript Answers

How to test an Express app with Mocha?

Sometimes, we want to test an Express app with Mocha.

In this article, we’ll look at how to test an Express app with Mocha.

How to test an Express app with Mocha?

To test an Express app with Mocha, we can make requests in our tests to the endpoint we’re testing with supertest.

For instance, we write

const request = require('supertest')
const {
  app
} = require('./express-app')
const assert = require("assert");

describe('POST /', () => {
  it('should fail', (done) => {
    request(app)
      .post('/')
      .send({
        'imgUri': 'foobar'
      })
      .expect(500)
      .end((err, res) => {
        done();
      })
  })
});

to call it with a callback with the test code.

In it, we call request with app to run the app.

And then we call post to make a POST request.

send sends the POST request body.

We then call expect to test the returned response code.

And then we call done in the end callback to finish the test.

Conclusion

To test an Express app with Mocha, we can make requests in our tests to the endpoint we’re testing with supertest.

Categories
JavaScript Answers

How to change default layout in Express using Handlebars?

Sometimes, we want to change default layout in Express using Handlebars.

In this article, we’ll look at how to change default layout in Express using Handlebars.

How to change default layout in Express using Handlebars?

To change default layout in Express using Handlebars, we can res.render with the layout option.

For instance, we write

res.render('view', {
  title: 'my other page',
  layout: 'other'
});

to call res.render with the layout set to other to render other.hbs as the layout file.

We can override the layout for the whole app with app.set:

app.set('view options', { layout: 'other' });

Conclusion

To change default layout in Express using Handlebars, we can res.render with the layout option.

Categories
JavaScript Answers

How to create a pair of private and public keys using Node.js crypto?

Sometimes, we want to create a pair of private and public keys using Node.js crypto.

In this article, we’ll look at how to create a pair of private and public keys using Node.js crypto.

How to create a pair of private and public keys using Node.js crypto?

To create a pair of private and public keys using Node.js crypto, we can use the generateKeyPair function.

For instance, we write

const {
  generateKeyPair
} = require('crypto');

generateKeyPair('rsa', {
  modulusLength: 4096,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem',
    cipher: 'aes-256-cbc',
    passphrase: 'top secret'
  }
}, (err, publicKey, privateKey) => {
  //...
});

to call generateKeyPair with 'rsa' to generate a pair of RSA keys.

We set the settings for the public key in publicKeyEncoding and set the settings for the private key in privateKeyEncoding.

Then we get the public and private from the publicKey and privateKey parameter in the callback

Conclusion

To create a pair of private and public keys using Node.js crypto, we can use the generateKeyPair function.

Categories
JavaScript Answers

How to get only the dataValues from Sequelize ORM?

Sometimes, we want to get only the dataValues from Sequelize ORM.

In this article, we’ll look at how to get only the dataValues from Sequelize ORM.

How to get only the dataValues from Sequelize ORM?

To get only the dataValues from Sequelize ORM, we can set the raw option to true.

For instance, we write

Model.findAll({
  raw: true,
  //...
});

to call findAll with an object with the raw property set to true to only return the dataValues in the query results.

Conclusion

To get only the dataValues from Sequelize ORM, we can set the raw option to true.