REST APIs

Express is the most popular Node web framework, which also serves as an underlying library for other popular Node web frameworks. It offers mechanisms for:

  • Write handlers for requests using various HTTP verbs at different URL paths/routes.

  • Establish standard web application settings, such as the port to use for connection and the location of templates used for response rendering.

  • Add additional request processing "middleware" at any point within the request handling pipeline.

The Namespace object provides an app method for the configuration of an Express app.

if (namespace.app) { // Express app for configuration
  // Write your Express Endpoints here.
  //For Example
  namespace.express('post', '/accept-cid', async (req, res) => {})
}

The namespace.express() method represents the Namespace wrapper over express app methods. It takes in 3 arguments:

  • method β€” This is the HTTP method: post, get, put, or delete.

  • path β€” This is the endpoint path appended to namespace.

  • callback β€” Callback function to be called.

Example:

namespace.express('post', '/accept-cid', async (req, res) => {
  try {
    const cid = req.body.cid;
    if (cid) {
      console.log('CID =' + cid);
      res.status(200).json({ message: 'CID' });
    }
  } catch (err) {
    console.log('CATCH IN ACCEPT CID', err);
  }
});

Express endpoints can be defined at the end of your executable file:

Last updated