Categories
JavaScript Answers

How to use Microsoft SQL Server use with Node.js?

Sometimes, we want to use Microsoft SQL Server use with Node.js.

In this article, we’ll look at how to use Microsoft SQL Server use with Node.js.

How to use Microsoft SQL Server with Node.js?

To use Microsoft SQL Server use with Node.js, we can use the node-mssql package.

To install it, we run

npm install mssql

Then we use it by writing

const sql = require("mssql");

const config = {
  server: "192.168.1.212",
  user: "test",
  password: "test",
};

sql.connect(config, (err) => {
  const request = new sql.Request();
  request.query("select 'hello world'", (err, recordset) => {
    console.log(recordset);
  });
});

We call sql.connect to make the database connection with the connection info specified in config.

Then in the callback, we make a sql.Request instance and call query on it to make a SQL query to the Microsoft SQL Server.

We call query with a SQL query string and a callback to get the returned results from recordset.

Conclusion

To use Microsoft SQL Server use with Node.js, we can use the node-mssql package.

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.