Categories
JavaScript Answers

How to create a quick file server with Node.js to server static files via HTTP?

Sometimes, we want to create a quick file server with Node.js to server static files via HTTP.

In this article, we’ll look at how to create a quick file server with Node.js to server static files via HTTP.

How to create a quick file server with Node.js to server static files via HTTP?

To create a quick file server with Node.js to server static files via HTTP, we can use the http, final-handler and serve-static packages.

For instance, we write

const http = require('http');
const finalhandler = require('finalhandler');
const serveStatic = require('serve-static');

const serve = serveStatic("./");

const server = http.createServer((req, res) => {
  const done = finalhandler(req, res);
  serve(req, res, done);
});

to call serveStatic with the path of the folder we want to serve files from.

Then we create the HTTP server with http.createServer with a callback that calls finalhandler to return the done function which runs when the request processing is done.

And then we call serve with the request req object, response res object, and the done function.

Conclusion

To create a quick file server with Node.js to server static files via HTTP, we can use the http, final-handler and serve-static packages.

Categories
JavaScript Answers

How to change a Node.js’s console font color?

Sometimes, we want to change a Node.js’s console font color.

In this article, we’ll look at how to change a Node.js’s console font color.

How to change a Node.js’s console font color?

To change a Node.js’s console font color, we can call console.log with the color code we want for the text.

For instance, we write

console.log('\x1b[36m%s\x1b[0m', 'I am cyan');
console.log('\x1b[33m%s\x1b[0m', stringToMakeYellow); 

to print cyan and yellow text respectivelt.

The first string argument is the color code.

The list of color codes and text styles include

Reset = "\x1b[0m"
Bright = "\x1b[1m"
Dim = "\x1b[2m"
Underscore = "\x1b[4m"
Blink = "\x1b[5m"
Reverse = "\x1b[7m"
Hidden = "\x1b[8m"

Foreground Black = "\x1b[30m"
Foreground Red = "\x1b[31m"
Foreground Green = "\x1b[32m"
Foreground Yellow = "\x1b[33m"
Foreground Blue = "\x1b[34m"
Foreground Magenta = "\x1b[35m"
Foreground Cyan = "\x1b[36m"
Foreground White = "\x1b[37m"

Background Black = "\x1b[40m"
Background Red = "\x1b[41m"
Background Green = "\x1b[42m"
Background Yellow = "\x1b[43m"
Background Blue = "\x1b[44m"
Background Magenta = "\x1b[45m"
Background Cyan = "\x1b[46m"
Background White = "\x1b[47m"

Conclusion

To change a Node.js’s console font color, we can call console.log with the color code we want for the text.

Categories
JavaScript Answers

How to convert an existing callback API to promises with Node.js?

Sometimes, we want to convert an existing callback API to promises with Node.js.

In this article, we’ll look at how to convert an existing callback API to promises with Node.js.

How to convert an existing callback API to promises with Node.js?

To convert an existing callback API to promises with Node.js, we can use the Promise constructor.

For instance, we write

const load = (param) => {
  return new Promise((resolve, reject) => {
    getStuff(param, (err, data) => {
      if (err !== null) {
        reject(err);
      } else {
        resolve(data);
      }
    });
  });
}

to create the load function that returns a promise that we create with the Promise constructor.

If there’s an error, err is set to a value, so we call reject with the err object.

Otherwise, we call resolve with the data that we get when the getStuff function succesfully runs.

Conclusion

To convert an existing callback API to promises with Node.js, we can use the Promise constructor.

Categories
JavaScript Answers

How to print to the console without a trailing newline with Node.js?

Sometimes, we want to print to the console without a trailing newline with Node.js.

In this article, we’ll look at how to print to the console without a trailing newline with Node.js.

How to print to the console without a trailing newline with Node.js?

To print to the console without a trailing newline with Node.js, we can use the process.stdout.write method.

For instance, we write

process.stdout.write(`Downloading ${data.length} bytes\r`);

to print Downloading ${data.length} bytes\r.

The \r character lets us overwrite the last line that was printed with new text that we print with process.stdout.write.

Conclusion

To print to the console without a trailing newline with Node.js, we can use the process.stdout.write method.

Categories
JavaScript Answers

How to do base64 encoding in Node.js?

Sometimes, we want to do base64 encoding in Node.js.

In this article, we’ll look at how to do base64 encoding in Node.js.

How to do base64 encoding in Node.js?

To do base64 encoding in Node.js, we can use the Buffer.from method.

For instance, we write

const b = Buffer.from("Hello World").toString('base64')

to call Buffer.from with the string we want to encode.

Then we convert the returned buffer object to a base64 string by calling toString with 'base64'.

Conclusion

To do base64 encoding in Node.js, we can use the Buffer.from method.