Endpoint request on the same server in express js

When processing a request on an explicit js server, I want to call an endpoint on the same server to fill in part of the response. Is there a way I can name the endpoint on the same server?

Sort of:

app.handle("/abc", {
    headers: {
    },
    params: {
    },
    type: "GET"
}, function (err, resp) {});
+4
source share
1 answer

You can do this using the supertest library (using it in the application, not in the test):

var handleRequest = require('supertest');

var request = handleRequest(app)[params.method](params.path)
.set('Accept', 'application/json')
.set(params.headers);

if (body) request.send(params.body);

request.end(function (err, resp) {
  console.log(resp.body);
});

where paramsis the object with the request parameters that you want to process, params.methodshould be a lowercase HTTP verb.

Alternatively, you can use mocks for request and response objects and call:

app.handle(reqMock, resMock, cb)
+1
source

Source: https://habr.com/ru/post/1542869/


All Articles