In most of my REST applications I use a message standardizer middleware, each message it handles I get from a json file. It's being great so far.
ResponseMiddleware.js:
import { getMessage } from "../common/messages.js";
const TYPE_JSON = "application/json";
const STATUS_CODE_OK = 200;
const STATUS_CODE_BAD_REQUEST = 400;
const STATUS_CODE_UNAUTHORIZED = 401;
const STATUS_CODE_NOT_FOUND = 404;
const STATUS_CODE_SERVER_ERROR = 500;
const jsonOK = function (data, message, metadata) {
const status = STATUS_CODE_OK;
data = data ? data : null;
message = message ? message : getMessage("default.successfulRequest");
metadata = metadata ? metadata : {};
this.status(status);
this.type(TYPE_JSON);
return this.json({ message, data, metadata, status: status });
};
const jsonBadRequest = function (data, message, metadata) {
const status = STATUS_CODE_BAD_REQUEST;
data = data ? data : null;
message = message ? message : getMessage("default.badRequest");
metadata = metadata ? metadata : {};
this.status(status);
this.type(TYPE_JSON);
return this.json({ message, data, metadata, status: status });
};
const jsonUnauthorized = function (data, message, metadata) {
const status = STATUS_CODE_UNAUTHORIZED;
data = data ? data : null;
message = message ? message : getMessage("default.unauthorized");
metadata = metadata ? metadata : {};
this.status(status);
this.type(TYPE_JSON);
return this.json({ message, data, metadata, status: status });
};
const jsonNotFound = function (data, message, metadata) {
const status = STATUS_CODE_NOT_FOUND;
data = data ? data : null;
message = message ? message : getMessage("default.notfound");
metadata = metadata ? metadata : {};
this.status(status);
this.type(TYPE_JSON);
return this.json({ message, data, metadata, status: status });
};
const jsonServerError = function (data, message, metadata) {
const status = STATUS_CODE_SERVER_ERROR;
data = data ? data : null;
message = message ? message : getMessage("default.server.error");
metadata = metadata ? metadata : {};
this.status(status);
this.type(TYPE_JSON);
return this.json({ message, data, metadata, status: status });
};
export const response = (req, res, next) => {
res.jsonOK = jsonOK;
res.jsonBadRequest = jsonBadRequest;
res.jsonUnauthorized = jsonUnauthorized;
res.jsonNotFound = jsonNotFound;
res.jsonServerError = jsonServerError;
next();
};
Messages.js
import fs from "fs";
import { dirname } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const messages = JSON.parse(fs.readFileSync(__dirname + "/message.json"));
export const getMessage = (path) => {
return messages[path] || null;
};
At least it was, until I cross with this question How do you share constants in NodeJS modules?. I didn't see nothing similar with what I've being doing, I keep researching about it and nobody is not even considering reading the constants from a json file.
Well, I think if every time to access the file the fs needs to open a connection and close it back, maybe the cost in this approach is too high. It would be better if fs could keep the connection open since it is going to need to use it all the time. But I want to state that I am not aware of how the fs package works.
Now comes the real question, where to keep the constants and why?