Categories
JavaScript Answers

How to serve static files on a dynamic route using Express?

Sometimes, we want to serve static files on a dynamic route using Express.

In this article, we’ll look at how to serve static files on a dynamic route using Express.

How to serve static files on a dynamic route using Express?

To serve static files on a dynamic route using Express, we can use the req.params property to get route parameters in our route handler.

For instance, we write

app.get("/user/:uid/files/*", (req, res) => {
  const uid = req.params.uid;
  const path = req.params[0] ? req.params[0] : "index.html";
  res.sendFile(path, { root: "./public" });
});

to get the value of the uid route parameter with req.params.uid.

And we get the value of the * parameter with req.params[0].

Finally, we call res.sendFile to return the file response for the file at the path relative to the root folder.

Conclusion

To serve static files on a dynamic route using Express, we can use the req.params property to get route parameters in our route handler.

Categories
JavaScript Answers

How to go back 1 folder level with __dirname in Node.js?

Sometimes, we want to go back 1 folder level with __dirname in Node.js.

In this article, we’ll look at how to go back 1 folder level with __dirname in Node.js.

How to go back 1 folder level with __dirname in Node.js?

To go back 1 folder level with __dirname in Node.js, we can use the path.join method.

For instance, we write

const reqPath = path.join(__dirname, "../../../");

to call path.join with __dirname and "../../../" to return the path of the folder that’s 3 levels above __dirname and assign it to reqPath.

Conclusion

To go back 1 folder level with __dirname in Node.js, we can use the path.join method.

Categories
JavaScript Answers

How to make synchronous requests in Node.js?

Sometimes, we want to make synchronous requests in Node.js.

In this article, we’ll look at how to make synchronous requests in Node.js.

How to make synchronous requests in Node.js?

To make synchronous requests in Node.js, we can use the sync-request package.

To install it, we run

npm i sync-request

Then we use it by writing

const request = require("sync-request");

try {
  const res = request("GET", url);
  const uComp = res.split("\n")[1].split(", ")[1];
  doSomething(uComp);
} catch (e) {}

to call request with 'GET' and url to make a GET request to the url.

Then we get the response with res.

Conclusion

To make synchronous requests in Node.js, we can use the sync-request package.

Categories
JavaScript Answers

How to get HTTP headers with Node.js?

Sometimes, we want to get HTTP headers with Node.js.

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

How to get HTTP headers with Node.js?

To get HTTP headers with Node.js, we can use the res.headers properties.

For instance, we write

const http = require("http");
const options = {
  method: "HEAD",
  host: "example.com",
  port: 80,
  path: "/",
};
const req = http.request(options, (res) => {
  console.log(JSON.stringify(res.headers));
});
req.end();

to call http.request to make a request.

We call it with the request options object and a callback that runs when the response is received.

Then we get the response headers for the request with res.headers.

Conclusion

To get HTTP headers with Node.js, we can use the res.headers properties.

Categories
JavaScript Answers

How to get number of processors available with Node.js?

Sometimes, we want to get number of processors available with Node.js.

In this article, we’ll look at how to get number of processors available with Node.js.

How to get number of processors available with Node.js?

To get number of processors available with Node.js, we can use the os.cpus method.

For instance, we write

const os = require("os");
const cpuCount = os.cpus().length;

to get the cpuCount with os.cpus().length.

Conclusion

To get number of processors available with Node.js, we can use the os.cpus method.