简体   繁体   中英

Make HTTP Request To Self in NodeJS / ExpressJS

I'm building an NPM module that needs to make an HTTP request to itself (the running web server). For example:

var url = "http://127.0.0.1:" + (process.env.PORT || 3000) + path;
request(url, function(error, response, body){ 
  ...
});

Is there a way to process a request through the NodeJS pipeline without actually doing an HTTP request?

Or is there a better way to form the URL? I'm nervous that 127.0.0.1 isn't the most robust way to handle this for production sites.

Self Consuming JSON API

In a self consuming JSON API, you define some functionality in some standalone controller functions and then wire the functionality up to express after the fact. Let's use a library application as an example:

books.js

module.exports = {
  browse: function () {
      return Book.findAll()
  },
  read: function (options) {
      return Book.findById(options.book)
  },
  processLateFees: function () {
      // Do a bunch of things to process late fees
  }
}

to-http.js

In this file we build a function that converts a controller function to an HTTP route. We take the query params and pass that to our controller as options:

module.exports = function toHTTP (func) {
    return function (req, res) {
         func(req.params).then(function (data) {
             res.send(data)
         })
    }
}

router.js

And then we connect up our controller to our http router

var express = require('express')
var books = require('./books')
var toHTTP = require('./to-http')

var app = express()
app.get('/books', toHTTP(books.browse))
app.get('/books/:book', toHTTP(books.read))
app.get('/batch-jobs/process-late-fees', toHTTP(books.processLateFees))

So we now have an express application connected up to controller functionality. And the wonderful thing is that we can call these controller functions manually too.

var books = require('./books')
books.processLateFees().then(function () {
    // late fees have been processed
})

If you need a more in depth example of this, the Ghost blog codebase is built around this pattern . It is a very informative read.

You can put that method inside your Model or Controller and call it inside the app if you just have one nodejs application, it needs fewer resources than to create a new request.

If you have more than one nodejs apps (or other services), it is normal to create a request to other web services with specific URL and port.

I do it in one of my project and that works fine. I use it in dev and prod without issues so far, because I use several nodejs applications with 3 differents web services that call themself to log in or check auhtentication. I use both express.js and sails.js (based on express.js)

我认为使用请求模块是可以接受的,它非常快,我在进行单元测试时使用这种方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM