1.1.4 • Published 4 years ago

express-methods v1.1.4

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

Express Methods

This project is build in TypeScript, demonstrates standard usage of express framework with advance custom request methods and error generators

Run application in Production mode

    npm start

Run application in Development mode Configured with Nodemon

    npm run dev

Finish Method

Finish method makes use of send method under the hood, finish method allows to send http response in a uniform pattern for all the APIS including success and failure response

const attachFinishMethod = (req: Request, res: ExpressResponse, next) => {
  let defaultHttpStatus = 200;

  if (req.method !== httpMethod.GET) {
    defaultHttpStatus = 201;
  }

  res.finish = (data = {}, message = "", httpStatus = defaultHttpStatus, errors = {}) => {
    res.status(httpStatus).send({
      message,
      data,
      errors
    });
  };
  next();
};

Make sure to add this middleware on highest scope of application, this will make sure to attach finish method to every request

app.use(attachFinishMethod);

Example for usage of finish method

app.get("/", (req, res: ExpressResponse, next) => {
  try {
    const result = {
      isUser: true
    };
    res.finish(result, "Fetched mock user info!");
  } catch (error) {
    next(error);
  }
});

Error Generators

ExpressError is build on top of Error constructor it adds ability to send custom http message and status code

throw new ExpressError("Its Forbidden", 403);
throw new httpError.Forbidden();

Make sure to place this middleware at the end of scope, this middleware is compatible with both the error generators shown above

app.use(expressErrorHandler);
1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago