Categories
JavaScript Answers

How to fix the “Typescript Error: Property ‘user’ does not exist on type ‘Request'” error with Express?

To fix the "Typescript Error: Property ‘user’ does not exist on type ‘Request’" error with Express, we can add our own definition for the request type.

For instance, we add

IGetUserAuthInfoRequest.ts

import { Request } from "express";
export interface IGetUserAuthInfoRequest extends Request {
  user: string;
}

into a TypeScript type definition file.

We create the IGetUserAuthInfoRequest interface the extends the Request interface by adding the user field.

Then we use it by writing

import { Response } from "express";
import { IGetUserAuthInfoRequest } from "./IGetUserAuthInfoRequest";

app.get(
  "/auth/userInfo",
  validateUser,
  (req: IGetUserAuthInfoRequest, res: Response) => {
    res.status(200).json(req.user);
  }
);

to import IGetUserAuthInfoRequest and use it as the type for the req parameter instead of Request.

Categories
JavaScript Answers

How to do server-side browser detection with Node.js?

Sometimes, we want to do server-side browser detection with Node.js

In this article, we’ll look at how to do server-side browser detection with Node.js.

How to do server-side browser detection with Node.js?

To do server-side browser detection with Node.js, we can use the ua-parser-js package.

To install it, we run

npm i ua-parser-js

Then we use it by writing

const UAParser = require("ua-parser-js");
//...
const ensureLatestBrowser = (req, res, next) => {
  const parser = new UAParser();
  const ua = req.headers["user-agent"];
  const browserName = parser.setUA(ua).getBrowser().name;
  const fullBrowserVersion = parser.setUA(ua).getBrowser().version;
  //...
  return next();
};

app.all(/^(?!(\/update)).*$/, ensureLatestBrowser);

to create a new UAParser instance.

Then we get the user-agent request header value with req.headers["user-agent"].

Next, we call setUA with the ua user agent string to parse the user agent string.

And then we call getBrowser on the parsed user agent object to get various parts of the user agent string to get the browser name, version, etc.

Conclusion

To do server-side browser detection with Node.js, we can use the ua-parser-js package.

Categories
JavaScript Answers

How to fix the writeFile ‘no such file or directory’ error in Node.js?

Sometimes, we want to fix the writeFile ‘no such file or directory’ error in Node.js.

In this article, we’ll look at how to fix the writeFile ‘no such file or directory’ error in Node.js.

How to fix the writeFile ‘no such file or directory’ error in Node.js?

To fix the writeFile ‘no such file or directory’ error in Node.js, we can call mkdirp to create the folder before we call writeFile to make sure the directory we’re writing to exists.

For instance, we write

const mkdirp = require("mkdirp");
const fs = require("fs");

const writeFile = async (path, content) => {
  await mkdirp(path);
  fs.writeFileSync(path, content);
};

to call mkdirp with the path to create the folder as the path if it doesn’t exist.

Then we call fs.writeFileSync to write the file to the path.

Conclusion

To fix the writeFile ‘no such file or directory’ error in Node.js, we can call mkdirp to create the folder before we call writeFile to make sure the directory we’re writing to exists.

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.