Categories
JavaScript Answers

How to return JSON using Node.js and Express?

Sometimes, we want to return JSON using Node.js and Express.

In this article, we’ll look at how to return JSON using Node.js and Express.

How to return JSON using Node.js and Express?

To return JSON using Node.js and Express, we can use the res.end and res.json method.

For instance, if we use the http package, we write

const http = require('http');

const app = http.createServer((req, res) => {
  res.setHeader('Content-Type', 'application/json');
  res.end(JSON.stringify({
    a: 1
  }, null, 3));
});
app.listen(3000);

to call http.createServer to create the server.

Then we call res.end with the stringified JSON object as the response.

With Express, we call app.json with

res.json({ a: 1 });

to return the JSON response with the object.

Conclusion

To return JSON using Node.js and Express, we can use the res.end and res.json method.

Categories
JavaScript Answers

How to enable HTTPS on Express.js and Node.js?

Sometimes, we want to enable HTTPS on Express.js and Node.js.

In this article, we’ll look at how to enable HTTPS on Express.js and Node.js.

How to enable HTTPS on Express.js and Node.js?

To enable HTTPS on Express.js and Node.js, we can read the certificate files and use them when we create the server.

For instance, we write

const fs = require('fs');
const http = require('http');
const https = require('https');
const privateKey = fs.readFileSync('sslcert/server.key', 'utf8');
const certificate = fs.readFileSync('sslcert/server.crt', 'utf8');

const credentials = {
  key: privateKey,
  cert: certificate
};
const express = require('express');
const app = express();

const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);

httpServer.listen(8080);
httpsServer.listen(8443);

to read the private key and certificate file with readFileSync.

Then we call createServer with the credentials object to let us run our web server with our certificate and key files via HTTPS.

And then we listen to the for HTTP and HTTPS requests with listen with different ports.

Conclusion

To enable HTTPS on Express.js and Node.js, we can read the certificate files and use them when we create the server.

Categories
JavaScript Answers

How to copy a file quickly in Node.js?

Sometimes, we want to copy a file quickly in Node.js.

In this article, we’ll look at how to copy a file quickly in Node.js.

How to copy a file quickly in Node.js?

To copy a file quickly in Node.js, we can use the fs.copyFile method.

For instance, we write

const fs = require('fs');

fs.copyFile('source.txt', 'destination.txt', (err) => {
  if (err) {
    throw err;
  }
  console.log('source.txt was copied to destination.txt');
});

to call copyFile with the path of the file to copy, the path to copy to, and a callback that’s run copyFile is done.

If err is set, that means an error occurred when copying.

Otherwise, the source file has been copied successfully.

Conclusion

To copy a file quickly in Node.js, we can use the fs.copyFile method.

Categories
JavaScript Answers

How to run a Node.js app as a background service?

Sometimes, we want to run a Node.js app as a background service.

In this article, we’ll look at how to run a Node.js app as a background service.

How to run a Node.js app as a background service?

To run a Node.js app as a background service, we can use the daemon module.

To install it, we run

npm i daemon

Then we use it by writing

const daemon = require('daemon');

daemon.daemonize({
  stdout: './log.log',
  stderr: './log.error.log'
}, './node.pid', (err, pid) => {
  if (err) {
    console.log(err);
    return process.exit(-1);
  }
  console.log(pid);
});

to call daemon.daemonize with an object with the path for stdout and stderr set to the log file path as the 1st argument.

The 2nd argument is the path to the script we want to run.

The 3rd argument is the callback that runs when daemon.daemonize is done.

If there’s an error, err will be set.

pid is the process ID and it’ll be set when the daemon is started.

Conclusion

To run a Node.js app as a background service, we can use the daemon module.

Categories
JavaScript Answers

How to print a stack trace in Node.js?

Sometimes, we want to print a stack trace in Node.js.

In this article, we’ll look at how to print a stack trace in Node.js.

How to print a stack trace in Node.js?

To print a stack trace in Node.js, we can get the stack property from the Error instance.

For instance, we write

const {
  stack
} = new Error()
console.log(stack)

to get the stack trace from the stack property of the Error instance.

We can also use console.trace method with no argument to print the stack trace.

Conclusion

To print a stack trace in Node.js, we can get the stack property from the Error instance.