Categories
JavaScript Answers

How to make a function wait until a callback has been called using Node.js?

Sometimes, we want to make a function wait until a callback has been called using Node.js.

In this article, we’ll look at how to make a function wait until a callback has been called using Node.js.

How to make a function wait until a callback has been called using Node.js?

To make a function wait until a callback has been called using Node.js, we can run our code in the callback that’s called when the operation is done.

For instance, we write

const f = (query, callback) => {
  myApi.exec('SomeCommand', (response) => {
    callback(response);
  });
}

to define the function f that takes the callback parameter.

We call that in the callback that’s called when myApi.exec is done.

We call callback with response as the argument to pass response to the callback.

Then we can call f to get the response value with

f(query, (returnValue) => {
  // ...
});

We get response from the returnValue.

Conclusion

To make a function wait until a callback has been called using Node.js, we can run our code in the callback that’s called when the operation is done.

Categories
JavaScript Answers

How to render basic HTML view with Express?

Sometimes, we want to render basic HTML view with Express.

In this article, we’ll look at how to render basic HTML view with Express.

How to render basic HTML view with Express?

To render basic HTML view with Express, we call app.use with express.static to serve a static folder.

For instance, we write

const express = require('express');
const app = express.createServer();

app.set("view options", {
  layout: false
});
app.use(express.static(__dirname + '/public'));

app.get('/', (req, res) => {
  res.render('index.html');
});

app.listen(8080, '127.0.0.1')

to call express.static with the static folder path in our project to serve that as the static folder.

Then we call res.render with a path to a file in the static folder to render that as the view content.

Conclusion

To render basic HTML view with Express, we call app.use with express.static to serve a static folder.

Categories
JavaScript Answers

How to format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js?

Sometimes, we want to format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js.

In this article, we’ll look at how to format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js.

How to format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js?

To format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js, we can use the toISOString method.

For instance, we write

new Date().toISOString()
  .replace(/T/, ' ')
  .replace(/\..+/, '')

to call toISOString on a Date instance.

Then we replace the T and the periods with a space and empty string respectively.

Conclusion

To format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js, we can use the toISOString method.

Categories
JavaScript Answers

How to redirect in Express.js while passing some context?

Sometimes, we want to redirect in Express.js while passing some context.

In this article, we’ll look at how to redirect in Express.js while passing some context.

How to redirect in Express.js while passing some context?

To redirect in Express.js while passing some context, we can call res.redirect with a query string appended to the path.

For instance, we write

app.get('/category', (req, res) => {
  const string = encodeURIComponent('baz baz');
  res.redirect('/?foo=' + string);
});

app.get('/', (req, res) => {
  const passedVariable = req.query.foo;
  // ...
});

to call encodeURIComponent in the /category route handler to encode the URL parameter value.

And then we call res.redirect with the URL path and query string appended after it.

Then in the / route handler, we get the value of the foo query parameter with req.query.foo.

Conclusion

To redirect in Express.js while passing some context, we can call res.redirect with a query string appended to the path.

Categories
JavaScript Answers

How to extract request HTTP headers from a request using Node.js Connect?

Sometimes, we want to extract request HTTP headers from a request using Node.js Connect.

In this article, we’ll look at how to extract request HTTP headers from a request using Node.js Connect.

How to extract request HTTP headers from a request using Node.js Connect?

To extract request HTTP headers from a request using Node.js Connect, we can use the req.headers property.

For instance, we write

const app = connect()
  .use(connect.logger('dev'))
  .use(connect.static('public'))
  .use((req, res) => {
    console.log(req.headers)
  })
  .listen(3000);

to get the request headers from the req.headers property in the 3rd middleware.

Conclusion

To extract request HTTP headers from a request using Node.js Connect, we can use the req.headers property.